【问题标题】:Spring Hibernate Validation @RequestBody春季休眠验证@RequestBody
【发布时间】:2017-01-27 20:02:47
【问题描述】:

使用 Spring Boot 1.4 和 Spring Data Rest/MVC 我无法让 @Valid @RequestBody 工作。我已经尝试了多个版本的 Hibernate Validator,声明了 Validtor bean 等。没有运气。

@BasePathAwareController
@RestController
public class TestRestController extends BaseController {
    @PostMapping("/pojo/save")
    public @ResponseBody ResponseEntity<?> upload(@Valid @RequestBody MyPojo pojo) {
     // Code here calling a repository.save. 
}

MyPojo 包含各种验证注释,即@Digits、@NotNull 等。当调用 repostiory.save 方法但 Spring 没有尝试对 pojo 进行任何验证时,我实际上看到了验证器异常。

这是我的 POM 部分,包括 spring 依赖项:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

任何想法将不胜感激。我进行了大量搜索并尝试了许多不同的配置,但没有任何运气。

更新

当尝试将实体保存到数据库时,就会发生验证错误。我收到以下错误:

Caused by: javax.persistence.RollbackException: Error while committing the transaction
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='numeric value out of bounds (<9 digits>.<3 digits> expected)...

【问题讨论】:

  • 如果您在前端使用 Spring 表单,请将 @RequestBody 更改为 @ModelAttribute...如果可能,请显示前端代码(您如何调用此请求)
  • 这是一个 REST 端点,所以我只是使用 Postman 客户端在正文中使用 JSON 对象进行 POST。
  • this 和 this link 可以帮助你
  • 我之前找到了这两个链接并尝试了这些解决方案。我还尝试了不同版本的休眠验证器和验证 api 库,但均无济于事。 Spring 拒绝验证请求正文。

标签: spring hibernate validation spring-boot


【解决方案1】:

所以事实证明,@BasePathAwareController 是导致绑定验证无法运行的原因。不确定这是预期行为还是错误,但通过从我的控制器中删除该注释,验证会按预期运行。

【讨论】:

    【解决方案2】:

    使用@Valid 注解,您应该在验证 pojo 之后立即声明 BindingResult 参数。 BindingResult 将包含所有验证错误,您可以随意使用它们。

    【讨论】:

    • 更多上下文:Getting Started - Validating Form Input。入门讨论了进行表单验证,但您也可以根据 JSON 请求进行自己的自定义响应。
    • 感谢您的回复。我也试过了,没有任何运气。 BindingResult 始终包含零错误。我还尝试创建一个 @ContainerAdvice 类来全局处理 MethodArgumentNotValidException 和 ConstraintViolationException,但这也没有奏效。
    • 尝试从参数中删除 @RequestBody 注释,并确保您的类路径中有 javax.validation 的实现,例如休眠验证器。你把BindingResult放在哪里了?它会在验证参数后立即生效,如下所示:(@Valid MyPojo pojo, BindingResult errors, @Valid MyAnotherPojo anotherPojo, BindingResult anotherErrors)
    • 我尝试删除 @RequestBody 注释,但仍然没有运气。我正在调试记录结果对象,这就是我得到的:在 save::pojo: MyPojo{id=null},结果:[]。我的方法签名是 save(@Valid MyPojo pojo, BindingResult result)
    • 什么?您的意思是,您正在将 BindingResult 添加到您的存储库方法中?!你应该把它添加到你的控制器方法中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 2016-03-24
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多