Does Spring MVC support for JSR-380 (Bean validation 2)?
Asked Answered
W

0

8

JSR-380 adds support for validating containers elements (inside collections, optional) by annotating the parameterized type:

List<@Email String> emails;

Is there any way to get this to work in current versions (e.g. Spring MVC 4.3.8) of Spring MVC on controller methods? I've upgraded to Hibernate Validator 6.0.2 and Validation API 2.0.0 but I can't seem to get the following examples to work and I'm not sure if it's my misuse of the annotations, missing configuration or a lack of support in the framework:

@RequestMapping(value = "/emails", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
public void emails(@Valid @RequestBody List<@Email String> emails) {

}

@RequestMapping(value = "/users", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
public void users(@Valid @RequestBody List<@Valid User> users) {

}   

(where User has @NotNull annotations on certain attributes)

I've tried with and without the @Valid before @RequestBody annotation.

Wessling answered 7/9, 2017 at 14:31 Comment(1)
Looks like it's only possible with Spring Framework 5. See github.com/spring-projects/spring-boot/issues/… and jira.spring.io/browse/SPR-13482Aeri

© 2022 - 2024 — McMap. All rights reserved.