Java 未为类参数调用约束验证器
我有一个带有类型参数的自定义类,约束验证器不会调用它。是否有方法调用类型参数的验证器 SpringController.javaJava 未为类参数调用约束验证器,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
@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;
@有效
受保护的收集实体;