【问题标题】:@NotNull annotation is not working in Spring boot application@NotNull 注解在 Spring Boot 应用程序中不起作用
【发布时间】:2019-10-01 10:08:00
【问题描述】:

下面是我的 DTO 课程。

public class AbstractDTO extends BaseDTO {

    private Integer createdBy;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_FORMAT)
    @NotNull(message = "createdDate may not be null")
    private LocalDateTime createdDate;

    private Integer lastModifiedBy;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_FORMAT)
    private LocalDateTime lastModifiedDate;

    private Boolean isActive;

    // getter & setters
}

在这里我试图将 createdDate 字段注释为 @NotNull 但它不起作用。它允许在请求正文中并且在邮递员中执行服务后没有收到任何错误。

我尝试了以下选项,但没有运气。

1) 尝试添加maven依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

2) 尝试将 DTO 类注解为 @Validated

3) 尝试用 @NotNull 注释 createdDate 字段 @Valid 但仍然不成功。

请帮我解决这个问题。

【问题讨论】:

  • 您是否尝试在控制器方法签名中将 DTO 类注释为 @Validated ?类似void foo(@Validated MyDTO dto)
  • @ArnaudDenoyelle 此 DTO 已在多个位置使用超过 25 个文件,我是否必须在每个文件中进行更改?还有其他解决办法吗?
  • 你的应用程序是否有启动器 - spring-boot-starter-web 它将加载休眠验证器到依赖项。这将验证 bean。请通过此链接 - stackoverflow.com/questions/48200444/…
  • 检查我的答案,它应该可以解决您的问题。但是,如果@NotNull 不起作用而@JsonPattern 起作用,则可能是使用了错误的@NotNull。否则,您似乎忘记在控制器方法中添加@Valid

标签: java spring spring-boot spring-mvc


【解决方案1】:

您的 DTO 类是正确的。你必须使用@Valid注解。

例如:

@Controller
public class Controller {

    @PostMapping("/")
    public String checkPersonInfo(@Valid AbstractDTO abstractDTO, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return "some-page";
        }
        return "some-other-page";
    }
}

参考此Spring Boot Example On Validating Form Input

为什么要使用@Valid注解?

这允许您验证应用于类的数据成员的一组约束。


但是,如果您的项目中有基于 XML 的配置,那么您必须将其添加到下面给出的 applicationContext.xml 中。 (来源:here

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean
            class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="validator" ref="validator" />
        </bean>
    </property>
</bean> 

    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    </bean>

【讨论】:

    【解决方案2】:

    您有一个带有一些请求正文的端点,例如;

    @RestController
    public class TheController {
    
        @PostMapping(path = "/doSomething", consumes = "application/json", produces = "application/json")
        public void post(@Valid @RequestBody AbstractDTO request) {
            //code
        }
    }
    

    您需要为请求对象添加@Valid 注释。只有这样,您才能为/doSomething 端点启用AbstractDTO 验证。

    查看here,了解更多详细信息

    【讨论】:

      【解决方案3】:

      你有正确的进口吗?
      我用import javax.validation.constraints.NotNull;

      【讨论】:

        猜你喜欢
        • 2021-06-04
        • 2019-07-03
        • 2019-04-08
        • 2021-06-02
        • 1970-01-01
        • 1970-01-01
        • 2018-03-15
        • 2021-08-01
        • 1970-01-01
        相关资源
        最近更新 更多