Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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
Java 未为类参数调用约束验证器_Java_Spring_Spring Mvc_Bean Validation_Generic Collections - Fatal编程技术网

Java 未为类参数调用约束验证器

Java 未为类参数调用约束验证器,java,spring,spring-mvc,bean-validation,generic-collections,Java,Spring,Spring Mvc,Bean Validation,Generic Collections,我有一个带有类型参数的自定义类,约束验证器不会调用它。是否有方法调用类型参数的验证器 SpringController.java @ResponseBody @RequestMapping(method = RequestMethod.POST) public EntityCollection<MyEntity> processData( @Valid @RequestBody EntityCollection<MyEntity> entityReque

我有一个带有类型参数的自定义类,约束验证器不会调用它。是否有方法调用类型参数的验证器

SpringController.java

@ResponseBody
@RequestMapping(method = RequestMethod.POST)
public EntityCollection<MyEntity> processData(
         @Valid @RequestBody EntityCollection<MyEntity> entityRequestSet
        , BindingResult bindingResult) throws Exception {
    if(bindingResult.hasErrors()) 
    {
        // Problem here is that bindingResult has no errors, even though MyEntity 
        // has nulls in it. If I use just MyEntity as RequestBody instead of 
        // EntityCollection<MyEntity>,  then the bindingResult has errors in it 
        // for fields with nulls
        MethodParameter parameter = new MethodParameter(this.getClass()
                .getMethod(new Object(){}.getClass().getEnclosingMethod().getName(), 
                EntityCollection.class, BindingResult.class), 0);
        throw new MethodArgumentNotValidException(parameter, bindingResult);
    }
    return null;
}
public class EntityCollection<MyEntity> extends GenericCollectionEntity<MyEntity> {

  public EntityCollection() {
      super();
  }

  public EntityCollection(
          Collection<MyEntity> entities) {
      super(entities);
  }
public abstract class GenericCollectionEntity<T> implements Serializable {

  private static final long serialVersionUID = 1L;

  public GenericCollectionEntity() {
      super();
  }

  public GenericCollectionEntity(Collection<T> entities) {
      super();
      this.entities = entities;
  }

  protected Collection<T> entities;

  public Collection<T> getEntities() {
      return entities;
  }

  public void setEntities(Collection<T> entities) {
      this.entities = entities;
  } 
}
@Entity
public class MyEntity implements Serializable {
private static final long serialVersionUID = 1L;

@Valid
@EmbeddedId
private EntityKey key;

  // getters & setters

}
@Embeddable
public class EntityKey implements Serializable {

  private static final long serialVersionUID = 1L;

  @NotNull
  @Column(name = "ID")
  private String id;
  // ommitted other fields
  //getters & setters
}
@Valid
protected Collection<T> entities;
EntityKey.java

@ResponseBody
@RequestMapping(method = RequestMethod.POST)
public EntityCollection<MyEntity> processData(
         @Valid @RequestBody EntityCollection<MyEntity> entityRequestSet
        , BindingResult bindingResult) throws Exception {
    if(bindingResult.hasErrors()) 
    {
        // Problem here is that bindingResult has no errors, even though MyEntity 
        // has nulls in it. If I use just MyEntity as RequestBody instead of 
        // EntityCollection<MyEntity>,  then the bindingResult has errors in it 
        // for fields with nulls
        MethodParameter parameter = new MethodParameter(this.getClass()
                .getMethod(new Object(){}.getClass().getEnclosingMethod().getName(), 
                EntityCollection.class, BindingResult.class), 0);
        throw new MethodArgumentNotValidException(parameter, bindingResult);
    }
    return null;
}
public class EntityCollection<MyEntity> extends GenericCollectionEntity<MyEntity> {

  public EntityCollection() {
      super();
  }

  public EntityCollection(
          Collection<MyEntity> entities) {
      super(entities);
  }
public abstract class GenericCollectionEntity<T> implements Serializable {

  private static final long serialVersionUID = 1L;

  public GenericCollectionEntity() {
      super();
  }

  public GenericCollectionEntity(Collection<T> entities) {
      super();
      this.entities = entities;
  }

  protected Collection<T> entities;

  public Collection<T> getEntities() {
      return entities;
  }

  public void setEntities(Collection<T> entities) {
      this.entities = entities;
  } 
}
@Entity
public class MyEntity implements Serializable {
private static final long serialVersionUID = 1L;

@Valid
@EmbeddedId
private EntityKey key;

  // getters & setters

}
@Embeddable
public class EntityKey implements Serializable {

  private static final long serialVersionUID = 1L;

  @NotNull
  @Column(name = "ID")
  private String id;
  // ommitted other fields
  //getters & setters
}
@Valid
protected Collection<T> entities;

我的缺点是,我不能把我的思想运用到基本的东西上。我刚刚将
@Valid
添加到抽象实体中的字段,它开始验证集合

GenericCollectionEntity.java

@ResponseBody
@RequestMapping(method = RequestMethod.POST)
public EntityCollection<MyEntity> processData(
         @Valid @RequestBody EntityCollection<MyEntity> entityRequestSet
        , BindingResult bindingResult) throws Exception {
    if(bindingResult.hasErrors()) 
    {
        // Problem here is that bindingResult has no errors, even though MyEntity 
        // has nulls in it. If I use just MyEntity as RequestBody instead of 
        // EntityCollection<MyEntity>,  then the bindingResult has errors in it 
        // for fields with nulls
        MethodParameter parameter = new MethodParameter(this.getClass()
                .getMethod(new Object(){}.getClass().getEnclosingMethod().getName(), 
                EntityCollection.class, BindingResult.class), 0);
        throw new MethodArgumentNotValidException(parameter, bindingResult);
    }
    return null;
}
public class EntityCollection<MyEntity> extends GenericCollectionEntity<MyEntity> {

  public EntityCollection() {
      super();
  }

  public EntityCollection(
          Collection<MyEntity> entities) {
      super(entities);
  }
public abstract class GenericCollectionEntity<T> implements Serializable {

  private static final long serialVersionUID = 1L;

  public GenericCollectionEntity() {
      super();
  }

  public GenericCollectionEntity(Collection<T> entities) {
      super();
      this.entities = entities;
  }

  protected Collection<T> entities;

  public Collection<T> getEntities() {
      return entities;
  }

  public void setEntities(Collection<T> entities) {
      this.entities = entities;
  } 
}
@Entity
public class MyEntity implements Serializable {
private static final long serialVersionUID = 1L;

@Valid
@EmbeddedId
private EntityKey key;

  // getters & setters

}
@Embeddable
public class EntityKey implements Serializable {

  private static final long serialVersionUID = 1L;

  @NotNull
  @Column(name = "ID")
  private String id;
  // ommitted other fields
  //getters & setters
}
@Valid
protected Collection<T> entities;
@有效
受保护的收集实体;