Spring MVC controller with multiple @RequestBody

后端 未结 1 499
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 04:54

I was wondering if for example a SpringMVC controller can have a method signature such as

@RequestMapping(value = \"/target\", method = RequestMethod.POST)
@         


        
相关标签:
1条回答
  • 2020-12-09 05:50

    Spring uses an interface called HandlerMethodArgumentResolver to decide which arguments it will pass to your handler methods. For parameters annotated with @RequestBody it uses a class called RequestResponseBodyMethodProcessor. This class basically looks in a set of HttpMessageConverter objects for one that can read the content-type of the request and can convert to the specified type. If it finds one, it passes the body of the HttpServletRequest as an InputStream to the HttpMessageConverter object.

    In this case, you will probably find some JSON deserializer doing work. It very likely (seeing the IOException you get) consuming the stream and then closing it.

    So really this way to do things isn't directly possible.

    One solution is to make a Filter that wraps the HttpServletRequest in your own implementation that buffers the InputStream to make it reusable/re-readable as many times as are required. But again the rules for deserializing from the body might be assumed by Spring and not be what you want exactly. In this case you can create your own Annotation and HandlerMethodArgumentResolver which you then register with the application in your configuration. You can then control exactly how things are deserialized from the request body.

    Another solution is to combine both MyObjectDto and messageBody into one DTO, if that makes sense to your data model (and to the Spring deserialization process).

    0 讨论(0)
提交回复
热议问题