Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Hibernate validator@NotEmpty无法工作spring boot和jackson_Spring Mvc_Jackson_Hibernate Validator_Spring Restcontroller - Fatal编程技术网

Spring mvc Hibernate validator@NotEmpty无法工作spring boot和jackson

Spring 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

我有一个类,它有@NotEmpty注释的errorRequests

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>