Spring mvc Hibernate validator@NotEmpty无法工作spring boot和jackson
我有一个类,它有@NotEmpty注释的errorRequestsSpring mvc Hibernate validator@NotEmpty无法工作spring boot和jackson,spring-mvc,jackson,hibernate-validator,spring-restcontroller,Spring Mvc,Jackson,Hibernate Validator,Spring Restcontroller,我有一个类,它有@NotEmpty注释的errorRequests public class ErrorsRequests implements Serializable { private static final long serialVersionUID = -727308651190295062L; private String applicationName; @NotEmpty @Valid @JsonProperty("errors") privat
public class ErrorsRequests implements Serializable {
private static final long serialVersionUID = -727308651190295062L;
private String applicationName;
@NotEmpty
@Valid
@JsonProperty("errors")
private List<ErrorsRequest> errorRequests;
@NotEmpty验证根本没有启动。Hibernate验证仅在json中包含errors元素时有效,如下所示
{
"errors": [
]
}
我可以让第一种情况也起作用吗?您以错误的方式使用注释,并且您的控制器也缺少注释
public class ErrorsRequests implements Serializable {
private static final long serialVersionUID = -727308651190295062L;
private String applicationName;
@NotEmpty
@Valid
@JsonProperty("errors")
private List<ErrorsRequest> errorRequests;
您应该使用@Validated:
@Validated
@RestController
@RequestMapping("/mycontroller")
public class MyController { ... }
在模型类中,您需要像这样放置注释:
@NotEmpty
private List<@Valid Foo> foo;
@NotEmpty
私人名单;
@Valid注释将检查列表中的项目是否也有效。检查您是否已在pom.xml中包含此依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
org.springframework.boot
弹簧启动启动器验证
也尝试了@NotBlank,但同样无效
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>