Hibernate spring mvc验证程序@valid';我不为收藏工作
我一直在使用SpringMVC和hibernate注释来验证传入的请求对象,在我需要验证传入的集合之前,这一切都很好Hibernate spring mvc验证程序@valid';我不为收藏工作,hibernate,validation,spring-mvc,collections,invoke,Hibernate,Validation,Spring Mvc,Collections,Invoke,我一直在使用SpringMVC和hibernate注释来验证传入的请求对象,在我需要验证传入的集合之前,这一切都很好 @RequestMapping(value = "/guests", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Set<GuestResource>> postGuestsToAttendance(
@RequestMapping(value = "/guests", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Set<GuestResource>> postGuestsToAttendance(
@Valid @RequestBody Set<RequestToAddGuest> guestRequests) throws FieldValidationException,
RequestBodyResourceBadRequestException
这是checkForErrors代码。当我只在一个单独的对象上使用@Valid时,效果非常好
protected void checkForErrors(BindingResult results) throws FieldValidationException {
if (results.hasErrors()) {
FieldValidationException exception = new FieldValidationException();
exception.setFieldErrors(results.getFieldErrors());
throw exception;
}
}
以下是带有验证注释的RequestToAddGuest类,仅供参考:
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.SafeHtml;
public class RequestToAddGuest {
@NotEmpty
@SafeHtml
public String firstName;
@SafeHtml
@NotEmpty
public String lastName;
@SafeHtml
public String emailAddress;
@SafeHtml
public String streetLine1;
@SafeHtml
public String streetLine2;
@SafeHtml
public String streetLine3;
@SafeHtml
public String city;
@SafeHtml
public String stateCode;
@SafeHtml
public String zip;
@SafeHtml
public String countryCode;
@SafeHtml
public String phoneArea;
@SafeHtml
public String phoneNumber;
@SafeHtml
public String phoneExtension;
}
好吧,我发现了问题。。。我被验证代码迷住了,忽略了跟踪变量名。。。我从未将验证调用更改为在单个guestRequest上调用,而不是在名为guestRequest*s的整个列表上调用*
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.SafeHtml;
public class RequestToAddGuest {
@NotEmpty
@SafeHtml
public String firstName;
@SafeHtml
@NotEmpty
public String lastName;
@SafeHtml
public String emailAddress;
@SafeHtml
public String streetLine1;
@SafeHtml
public String streetLine2;
@SafeHtml
public String streetLine3;
@SafeHtml
public String city;
@SafeHtml
public String stateCode;
@SafeHtml
public String zip;
@SafeHtml
public String countryCode;
@SafeHtml
public String phoneArea;
@SafeHtml
public String phoneNumber;
@SafeHtml
public String phoneExtension;
}