Json Spring:如何在ArrayList中验证bean

Json Spring:如何在ArrayList中验证bean,json,hibernate,spring-mvc,jackson,bean-validation,Json,Hibernate,Spring Mvc,Jackson,Bean Validation,我有以下映射处理程序: @Controller @RequestMapping("/product") public class ProductController { @RequestMapping(value = "update", method = RequestMethod.POST) @ResponseBody public Map<String, ?> update(@Valid @RequestBody ListOfProduct produc

我有以下映射处理程序:

@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
}
它应该违反“name”属性的constraint@Size

它应该由控制器内部的handleMethodArgumentNotValidException()处理

但是,我得到了一个例外:

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}'}
]
有什么建议可以解决这个问题吗


非常感谢。

日志中的异常是一个
javax.validation.ConstraintViolationException
,但您的异常处理程序是针对异常类型
MethodArgumentNotValidException
。我认为您应该尝试在@RequestBody上声明
ConstraintViolationException

@的处理程序有效,这确实会导致MethodArgumentNotValidException。尝试在RequestResponseBodyMethodProcessor第74行中插入中断符。如果您认为有问题,请在JIRA中报告。

是的,您是对的。但是它应该抛出
MethodArgumentNotValidException
。如果抛出
ConstraintViolationException
而不是
MethodArgumentNotValidException
,这意味着
update()
方法中的
@Valid
注释是无用的(验证不是通过spring完成的)。除非您不是通过其他方式调用验证,否则我认为验证实际上是由spring触发的(请注意,Spring MVC也在3.0.x中执行了由
@Valid
触发的验证,而
MethodArgumentNotValidException
刚刚在Spring 3.1中引入)。我无法告诉thoughm哪个配置/设置导致抛出哪个异常。
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}'}
]