【问题标题】:Is it possible to use @Valid in Service layer? What could be the workaround?是否可以在服务层使用@Valid?解决方法是什么?
【发布时间】:2019-11-29 07:14:21
【问题描述】:

我正在编写一个验证服务,我不需要直接验证传入的请求对象,而是在经过一些操作后对其进行验证。

 @Service
 public class FooCreationService {
                private String validateFoo(@Valid Foo foo) {    
                    return "";
                }
            }



  public class Foo {
        @Size(min=1, max=60)
        @NotBlank(message="Product description required.")
        private String fooDescription;
    }

【问题讨论】:

    标签: spring spring-boot validation spring-mvc


    【解决方案1】:

    当 Spring Boot 找到一个带有 @Valid 注释的参数时,它会自动引导默认的 JSR 380 实现——Hibernate Validator——并验证该参数。 当目标参数验证失败时,Spring Boot 会抛出 MethodArgumentNotValidException 异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      相关资源
      最近更新 更多