我已经添加了我的自定义注释及其约束验证器来验证PersonForm上的列表输入,但是当涉及到测试异常触发时它会处理数据绑定 我的请求发送了ajax call + form.serialize();
为personForm:
private Long id;
@NotEmpty
private String firstName;
@NotEmpty
private String lastName;
@Email(message = "profile.email.email")
private String email;
private List<@ResourceConstraint String> resource;
// all setters/getters ....
注释
@Documented
@Constraint(validatedBy = ResourceValidator.class)
@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@NotEmpty
public @interface ResourceConstraint {
String message() default "resource not Valid";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
约束验证器
public class ResourceValidator implements ConstraintValidator<ResourceConstraint, String> {
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
boolean result = false;
if(Enums.getIfPresent(Resource.class, value.toUpperCase()).isPresent()) {
result = true;
}
return result;
}
}
控制器
@PostMapping(path = "/applay/profile/{id}", produces = "application/json;charset=UTF-8")
public @ResponseBody JsonResponseBean applayProfile(@PathVariable Long id, @Valid PersonForm form, BindingResult result, Locale locale) throws BusinessException {
JsonResponseBean jsonResponse = new JsonResponseBean();
Person current = new Person();
if (!result.hasErrors()) {
...
} else {
...
}
return jsonResponse;
}
例外
java.lang.IllegalStateException: JSR-303 validated property 'resource[1].<list element>' does not have a corresponding accessor for Spring data binding - check your DataBinder's configuration (bean property versus direct field access)
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.processConstraintViolations(SpringValidatorAdapter.java:162)
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:117)
at org.springframework.validation.DataBinder.validate(DataBinder.java:891)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.validateIfApplicable(ModelAttributeMethodProcessor.java:168)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:115)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'resource[1].<list element>' of bean class [com.domain.project.forms.PersonForm]: Bean property 'resource[1].<list element>' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:633)
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:624)
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:99)
at org.springframework.validation.AbstractBindingResult.getRawFieldValue(AbstractBindingResult.java:283)
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.getRejectedValue(SpringValidatorAdapter.java:268)
at org.springframework.validation.beanvalidation.SpringValidatorAdapter.processConstraintViolations(SpringValidatorAdapter.java:148)
... 40 common frames omitted
如何正确绑定自定义验证程序进程 PersonForm的所有setter和getter都已实现 根据{{3}}我不需要任何东西,只需应用约束
依赖(Gradle)
compile group: 'javax.validation', name: 'validation-api', version: '2.0.0.Final'
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.2.Final'
...
答案 0 :(得分:0)
我明白了,摆脱这个错误升级到
spring Version = '5.0.1.RELEASE'
hibernate Version = '5.2.12.Final'
with
compile group: 'javax.validation', name: 'validation-api', version: '2.0.0.Final'
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.4.Final'