以下是我的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,但它不起作用。它在请求体中被允许,在postman中执行服务后也没有出现任何错误。
我尝试了以下选项,但没有成功。
1)尝试添加maven依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
2)尝试将DTO类注释为@Validated
3)尝试使用@NotNull注释createdDate字段,但仍然没有运气。
请帮我解决这个问题。
void foo(@Validated MyDTO dto)
- Arnaud Denoyelle@NotNull
无效而@JsonPattern
有效,则可能是使用了错误的@NotNull
。否则,您可能忘记在控制器方法上添加@Valid
。 - buræquete