我面临一个Spring验证的奇怪问题。我正在尝试使用Integration测试来使用Spring anotation验证bean。但是下面是我面临的不同实现的问题。
1)当我尝试@Valid @RequestBody TaskApprovalDetails taskApprovalDetails(TaskApprovalDetails是我的bean)时,我得到以下异常
引起:javax.el.ELException:找不到提供者com.sun.el.ExpressionFactoryImpl 在javax.el.FactoryFinder.newInstance(FactoryFinder.java:101) 在javax.el.FactoryFinder.find(FactoryFinder.java:197) 在javax.el.ExpressionFactory.newInstance(ExpressionFactory.java:197) 在javax.el.ExpressionFactory.newInstance(ExpressionFactory.java:168) 在org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTerm。(InterpolationTerm.java:59)
Please note without @Valid, @RequestBody is able to map my json to taskApprovalDetails.
2)当我尝试@Valid @ModelAttribute(" taskApprovalDetails")TaskApprovalDetails taskApprovalDetails我没有得到任何异常。我能够访问我的控制器,但taskApprovalDetails对象中的所有属性都为null。 @ModelAttribute无法将我的请求json映射到objet
以下是一些示例代码:
控制器:
@RequestMapping(value = "/{taskId}/{customerId}/accept", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(OK)
public void acceptTask(@PathVariable String taskId, @PathVariable String customerId, @RequestParam("user") String user,
@Valid @ModelAttribute("taskApprovalDetails") TaskApprovalDetails taskApprovalDetails) throws URISyntaxException, ParseException {
inboxService.acceptTask(taskApprovalDetails, user, taskId, customerId);
}
或
@RequestMapping(value = "/{taskId}/{customerId}/accept", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(OK)
public void acceptTask(@PathVariable String taskId, @PathVariable String customerId, @RequestParam("user") String user,
@Valid @RequestBody TaskApprovalDetails taskApprovalDetails) throws URISyntaxException, ParseException {
inboxService.acceptTask(taskApprovalDetails, user, taskId, customerId);
}
JSON
public static String mockJsonRequestBody(final String customerId, final String role) {
return "{\"comments\":\"Click\",\"role\":\"User\",\"userId\":\"asdasdasd\"}";
}
TaskApprovalDetails.class
@JsonInclude(NON_NULL)
public class TaskApprovalDetails {
private int rowId;
private String role;
@NotBlank
@Size(min=20, max = 25)
private String userId;
private Date taskCreatedDate;
private Date taskCompletedDate;
private String decision;
private BigDecimal level;
@Size(min = 20, max = 25, message = "Comments can be between 20-25 char only.")
private String comments;
private String rejectedSection;
private List<String> businessRestriction;
请告诉我哪种方法可以验证bean的最佳方法。 @ModelAtrubute或@RequestBody以及如何解决上述问题。