3

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you're right. But it's supposed to throw MethodArgumentNotValidException. If ConstraintViolationException is thrown instead of MethodArgumentNotValidException, it means that @Valid annotation in update() method is useless (validation is not done via spring).
Unless you aren't invoking validation by other means I'd think validation actually is triggered by Spring (note that Spring MVC performed a validation triggered by @Valid also in 3.0.x, while MethodArgumentNotValidException was just introduced with Spring 3.1). I can't tell thoughm which configuration/set-up causes which exception to be thrown.
1

@Valid on an @RequestBody should indeed result in a MethodArgumentNotValidException. Try putting a break in RequestResponseBodyMethodProcessor line 74. If you believe there is an issue report it in JIRA.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.