search

Home  >  Q&A  >  body text

java - 方法形参上可以用多个@RequestBody吗,封装不同的对象

在方法的形参那可以用多个@RequestBody来把json字符串封装成多个对象么?用一个@RequestBody.肯定可以,多个这个注解可以封装多个不同的对象么……

天蓬老师天蓬老师2802 days ago1668

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-18 10:57:14

    In fact, there is always one request parameter, because a request only contains one request body. If you understand this, you will understand that Spring MVC does not support multiple @RequestBody.

    As for deserializing the content in a request body into several Java instances, it is another problem.
    There are three solution directions:

    1. Create a new entity and put your two entities in it. This is the simplest, but not "elegant" enough.

    2. Use Map<String, Object> to accept the request body and deserialize it into each entity yourself.

    3. Similar to method 2, but more generic, implement your own HandlerMethodArgumentResolver. Please refer to: https://sdqali.in/blog/2016/0...

    reply
    0
  • 阿神

    阿神2017-04-18 10:57:14

    Let’s list a business scenario

    reply
    0
  • Cancelreply