springmvc processes multiple @ requestbodies

Connection in this article

New content Explain time Is it finished?
Processing multiple parameters Use @ RequestBody like @ RequestParam 20200320 Hang in the air
Parameter must be filled The background hopes that some parameters must be passed. For example, @ RequestParam does not pass parameters but only reports an error but does not prompt that parameter. It is even powerless for POJO type --:-- Hang in the air
Parameter regular check An extension required for parameter --:-- Hang in the air

background

The technology stack is replaced with spring cloud, and openFeign is used between services to declare that httpclients call each other. In order to achieve elegant parameter passing, no map or a POJO is used for parameter passing

Occupied pit

The controller does not want to use a POJO to receive the processed message. It uses @ RequestBody just like @ RequestParam

Understanding feign

feign client

feign is not covered here. This article describes how to deal with multiple @ requestbodies
@FeignClient(value = "cloud-zuul", configuration = {FeignSupportConfig.class, FeignConfig.class})
public interface IUserService {

    @RequestMapping(value = "api-a/getUser", method = RequestMethod.POST, consumes = "application/json")
    String getUserRequestBody_(@RequestBody User[] userArray2);

    @RequestMapping(value = "api-a/getUser", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    String getUserComplex_(@CustomRequestParam("user") User user, @CustomRequestParam("dept") Dept dept,  @RequestParam("schoolId") String schoolId,
                           @CustomRequestParam(value = "userArray") User[] userArray, @CustomRequestParam(value = "deptList") List<Dept> deptList);


}

request body

{"dept":{"deptId":"deptId","deptName":"deptName"},"userArray":[{"userId":"userId","userName":"userName"}],"deptList":[{"deptId":"deptId","deptName":"deptName"}]}]

code

NOTE!!! Of course, @ RequestBody is not used directly here. Here is a demonstration of how to use
@PostMapping("/queryUserList")
public JSONObject queryUserList(@RequestBody("userList") List<User> userList, @RequestBody("user") User user){

	JSONObject jo = new JSONObject();

	jo.put("userList", userList);
	jo.put("user", user);
	return jo;
}

Tags: Programming Spring JSON

Posted on Fri, 20 Mar 2020 14:52:36 -0400 by fitzsic