Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring : How to Validate Beans inside ArrayList
    text
    copied!<p>I have the following mapping handler:</p> <pre><code>@Controller @RequestMapping("/product") public class ProductController { @RequestMapping(value = "update", method = RequestMethod.POST) @ResponseBody public Map&lt;String, ?&gt; update(@Valid @RequestBody ListOfProduct productList) { if (productDao.saveOrUpdateAll(productList)) { return jsonUtils.statusMessage(true, "Updated successfully."); } return jsonUtils.statusMessage(false, "Failed."); } @ExceptionHandler @ResponseStatus(value = HttpStatus.BAD_REQUEST) @ResponseBody public String handleMethodArgumentNotValidException(MethodArgumentNotValidException error) { return "Bad request: " + error.getMessage(); } public static class ListOfProduct extends ArrayList&lt;Product&gt; { private static final long serialVersionUID = 1L; public ListOfProduct() { super(); } } } @Entity public class Product implements Serializable { private static final long serialVersionUID = 1L; @Min(1) private int id; @NotNull @Size(min = 5) private String name; public product() { } // getters and setters } </code></pre> <p>I'm trying to validate this JSON:</p> <pre><code>[{"id":6, "name":""}] </code></pre> <p>It is supposed to be a violation for constraint @Size of "name" property.</p> <p>And it is supposed to be handled by <strong>handleMethodArgumentNotValidException()</strong> inside the controller.</p> <p>But instead, I got this exception:</p> <pre><code>Jan 21, 2012 10:25:00 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [appServlet] in context with path [/WebTest] threw exception [Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.tes.web.entity.Product] during update time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='size must be between 5 and 2147483647', propertyPath=name, rootBeanClass=class com.tes.web.entity.Product, messageTemplate='{javax.validation.constraints.Size.message}'} ]] with root cause javax.validation.ConstraintViolationException: Validation failed for classes [com.tes.web.entity.Product] during update time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='size must be between 5 and 2147483647', propertyPath=name, rootBeanClass=class com.tes.web.entity.Product, messageTemplate='{javax.validation.constraints.Size.message}'} ] </code></pre> <p>Any suggestion how to solve this?</p> <p>Thank you very much.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload