如何将 @Valid 与 Spring MVC 的 @RequestBody 参数一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6129285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:30:42  来源:igfitidea点击:

How do I use @Valid with Spring MVC's @RequestBody parameters?

springvalidationdata-bindingspring-mvcbean-validation

提问by Hank Gay

I'm having an issue with using @Validon a parameter to a handler method on my @Controller. My code looks like this:

@Valid在将参数用于我的@Controller. 我的代码如下所示:

@RequestMapping(value=BIBBLE_BOBBLE_URI_PATTERN + "/widgets", method=RequestMethod.POST)
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
public Widget saveNewWidget(
        @PathVariable final String username,
        @PathVariable final String bibbleBobbleName,
        @Valid @RequestBody final Widget widget,
        final BindingResult results,
        final HttpServletRequest request,
        final HttpServletResponse response)

where Widgetis the class for one of my domain objects. I'm using the @RequestBodyannotation to indicate that the payload of the request maps to widget(in my tests, the requests are JSON, though Hymanson is on my classpath so I could also use XML).

Widget我的域对象之一的类在哪里。我使用@RequestBody注释来指示请求的有效负载映射到widget(在我的测试中,请求是 JSON,虽然 Hymanson 在我的类路径上,所以我也可以使用 XML)。

As you can see, the BindingResultparameter follows directly after the Widgetparameter, but I get the following error:

如您所见,BindingResult参数紧跟在Widget参数之后,但出现以下错误:

java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!

How do I apply the @Validannotation to a @RequestBodyparameter and then get the results?

如何将@Valid注释应用于@RequestBody参数然后获得结果?

P.S. I'm using annotation-drivento handle wiring up the controllers, do content-negotiation, etc.

PS我annotation-driven用来处理控制器的接线,进行内容协商等。

采纳答案by Ritesh

Are you using Spring 3.1? It is a newly added feature in Spring version 3.1. Please see Validation For @RequestBody Method Arguments

您使用的是 Spring 3.1 吗?它是 Spring 3.1 版本中新增的功能。请参阅@RequestBody 方法参数的验证

回答by Chandra

This is an issue in spring 3.0 and it is fixed in 3.1 M2.

这是 spring 3.0 中的一个问题,它已在 3.1 M2 中修复。

https://jira.springsource.org/browse/SPR-6709

https://jira.springsource.org/browse/SPR-6709

If you are using spring 3.0, based on some threads and extensive reading, here is the best approach for this problem. An aspect oriented approach so that we can back out when you upgrade it to the version of spring which fixes this issue.

如果您使用的是 spring 3.0,根据一些线程和广泛阅读,这里是解决此问题的最佳方法。一种面向方面的方法,以便我们可以在您将其升级到修复此问题的 spring 版本时退出。

An aspect which intercepts calls to method which uses @RequestMapping annotation and for each parameter which has @Valid annotation on it, call the corresponding validator in the aspect.

一个方面拦截对使用@RequestMapping 注释的方法的调用,并且对于每个带有@Valid 注释的参数,调用方面中相应的验证器。