简体   繁体   English

Spring MVC中的条件@RequestBody

[英]Conditional @RequestBody in Spring MVC

I have a plain Spring MVC controller that returns a Model and View. 我有一个普通的Spring MVC控制器,它返回模型和视图。 This works great. 这很好。 But now we are moving over to pure REST architecture where the server only communicates via JSON. 但是现在我们正在转向纯REST架构,其中服务器仅通过JSON进行通信。

Instead of revamping everything, I am trying to resuse the existing controllers as much as i could and here is what i am able to do 我没有重做一切,而是尝试尽可能地重用现有的控制器,这就是我能够做到的

I am able to reuse the GET requests to same controllers by including a ContentNegotiatingViewResolver . 我可以通过包含ContentNegotiatingViewResolverGET请求重用到相同的控制器。 So now the same controller can return a jsp and a model converted into json based on accept header . 因此,现在同一控制器可以返回一个jsp和一个基于accept标头转换为json的模型。

Now the other part is POST requests that i need some help on. 现在另一部分是POST请求,我需要一些帮助。

To bind a json request I ll have to use @RequestBody on the existing controllers but I want to do it conditionally in the same way as i did for GET requests. 要绑定一个json请求,我将不得不在现有的控制器上使用@RequestBody,但是我想有条件地像使用GET请求一样使用它。 So if the accept header is json it should use that message converter to bind the model otherwise use the usual html form binding. 因此,如果accept报头是json,则应使用该消息转换器绑定模型,否则请使用常规的html表单绑定。

And after this is done, how to handle BindingResult object for my scenario? 这样做之后,如何为我的场景处理BindingResult对象?

To summarize here are my questions: 总结一下,这是我的问题:

  1. Can we conditionally use @RequestBody based on Accept header 我们能否基于Accept标头有条件地使用@RequestBody
  2. Is there any configuration that automatically converts your Form binding errors object (inside a Spring MVC controller) to a json response based on Accept header. 是否有任何配置可以自动将您的Form绑定错误对象(在Spring MVC控制器内部)转换为基于Accept标头的json响应。

Look into the consumes and produces attributes of the @RequestMapping attribute. 查看@RequestMapping属性的consumesproduces属性。 You could have two versions of the same method with different consume s, one of which can take a @RequestBody and one that doesn't. 您可能具有相同方法的两个版本,具有不同的consume s,其中一个可以使用@RequestBody而另一个不能。

Can't really help you with the second part of your question, sorry. 对不起,您真的无法帮助您解决问题的第二部分。

To handle the POST requests you will need to have a custom AbstractHttpMessageConverter and a ModelAttributeMethodProcessor . 要处理POST请求,您将需要具有自定义的AbstractHttpMessageConverterModelAttributeMethodProcessor You need the MessageConverter to handle the controllers that do use request body ( @RequestBody ) and the ModelAttributeMethodProcessor to handle the validation if the controller is using request parameters ( @RequestParam ). 如果控制器正在使用请求参数( @RequestParam ),则需要MessageConverter来处理确实使用请求主体的控制器( @RequestBody ),并需要ModelAttributeMethodProcessor来处理验证。

Obviously you'll want to check for a header or something otherwise do the default binding. 显然,您将需要检查标题或其他方式进行默认绑定。

I answered a different question about validating immutable objects but the same method applies. 我回答了另一个有关验证不可变对象的问题,但适用相同的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM