JSR - 使用Spring Boot

时间:2017-06-02 04:15:20

标签: java spring-mvc spring-boot bean-validation jsr

我使用的是Spring Boot 1.5.2.RELEASE,并且无法为@RequestParam&成功使用JSR-349(bean验证1.1)。方法本身@PathVariable

对于POST请求,如果method参数是Java POJO,那么使用@Valid注释该参数工作正常但注释@RequestParam& @PathVariable@NotEmpty@Email无效。@Validated

我使用Spring的validation-api-1.1.0.Final.jar

注释了控制器类

关于SO有很多问题,我对this answer评论说它不适合我。

Spring Boot包括 - hibernate-validator-5.3.4.Final.jar@RequestMapping(method = RequestMethod.GET, value = "/testValidated", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseBean<String> testValidated(@Email @NotEmpty @RequestParam("email") String email) { ResponseBean<String> response = new ResponseBean<>(); response.setResponse(Constants.SUCCESS); response.setMessage("testValidated"); logger.error("Validator Not called"); return response; }

我错过了什么吗?

示例代码,

email

当我发送空值或testValidated&amp;的完整电子邮件地址时,永远不会调用处理程序。控件总是采用@ExceptionHandler(ConstraintViolationException.class) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public ResponseBean handle(ConstraintViolationException exception) { StringBuilder messages = new StringBuilder(); ResponseBean response = new ResponseBean(); exception.getConstraintViolations().forEach(entry -> messages.append(entry.getMessage() + "\n")); response.setResponse(Constants.FAILURE); response.setErrorcode(Constants.ERROR_CODE_BAD_REQUEST); response.setMessage(messages.toString()); return response; } 方法。

ResponseBean<T>

<svg>是我的应用程序特定类。

2 个答案:

答案 0 :(得分:11)

经过两天以上的失败后,我问过这个问题&amp;审判。由于围绕Spring Validations和JSR验证的混淆,Spring如何调用JSR验证器,JSR标准和JS的变化,有很多令人困惑的答案。支持的验证类型。

最后,this article帮了很多忙。

我分两步解决了问题,

1.将以下bean添加到我的Configuration - 没有这些bean,没有任何作用。

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    MethodValidationPostProcessor mvProcessor = new MethodValidationPostProcessor();
    mvProcessor.setValidator(validator());
    return mvProcessor;
}           

@Bean
public LocalValidatorFactoryBean validator() {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.setProviderClass(HibernateValidator.class);
    validator.afterPropertiesSet();
    return validator;
}

2.在我的控制器上放置Spring的@Validated注释,如下所示,

@RestController
@RequestMapping("/...")
@Validated
public class MyRestController {
}

经过验证的是 - org.springframework.validation.annotation.Validated

此设置不会影响同一控制器中@Valid验证的@RequestBody注释,并且这些注释会继续有效。

现在,我可以为MyRestController class,

中的方法触发如下所示的验证
@RequestMapping(method = RequestMethod.GET, value = "/testValidated" , consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseBean<String> testValidated(
        @Email(message="email RequestParam is not a valid email address") 
        @NotEmpty(message="email RequestParam is empty") 
        @RequestParam("email") String email) {
    ResponseBean<String> response = new ResponseBean<>();
    ....
    return response; 
}

我必须在异常处理程序中为异常添加另一个处理程序 - ConstraintViolationException,因为@Validated会抛出此异常而@Valid抛出MethodArgumentNotValidException

答案 1 :(得分:0)

添加@Validated时,Spring @Validated @Controller没有映射。从控制器本身删除任何继承确实有帮助。否则Sabir Khan的回答起作用并且有所帮助。