Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring : How to Validate Beans inside ArrayList

I have the following mapping handler:

@Controller
@RequestMapping("/product")
public class ProductController {

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, ?> 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<Product> {
        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
}

I'm trying to validate this JSON:

[{"id":6, "name":""}]

It is supposed to be a violation for constraint @Size of "name" property.

And it is supposed to be handled by handleMethodArgumentNotValidException() inside the controller.

But instead, I got this exception:

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}'}
]

Any suggestion how to solve this?

Thank you very much.

like image 638
Fitrah M Avatar asked Dec 07 '25 12:12

Fitrah M


1 Answers

The exception in your log is a javax.validation.ConstraintViolationException, but your exception handler is for the exception type MethodArgumentNotValidException. I think you should try to declare your handler for ConstraintViolationException.

like image 189
Gunnar Avatar answered Dec 09 '25 16:12

Gunnar