[英]Spring+Hibernate validation: throws Hibernate Exceptions and not Spring exceptions
I have a simple project (details below) which has a Spring bean and uses HV are validation provider, used for method parameter validation. 我有一个简单的项目(详见下文),它有一个Spring bean,并使用HV作为验证提供程序,用于方法参数验证。
Problem: When validation fails, it throws Hibernate exception ( org.hibernate.validator.method.MethodConstraintViolationException
). 问题:验证失败时,会抛出Hibernate异常( org.hibernate.validator.method.MethodConstraintViolationException
)。 But I would expect it to throw Spring exception ( org.springframework.web.bind.MethodArgumentNotValidException
), since Spring is the wrapping interface. 但我希望它抛出Spring异常( org.springframework.web.bind.MethodArgumentNotValidException
),因为Spring是包装接口。 For all I know, I do not care who is the validation implementor and need to deal with only Spring defined classes. 据我所知,我不关心谁是验证实现者,只需要处理Spring定义的类。
Question 1: Is the above argument right and should it throw Spring exceptions? 问题1:上述论点是否正确,是否应该抛出Spring异常?
Question 2: If Hibernate exception is the norm, how do I map it to a friendly message (could not find this info on Google) 问题2:如果Hibernate异常是常态,我如何将其映射到友好消息(在Google上找不到此信息)
ProductManager.java: ProductManager.java:
@Component
@Validated
public class ProductManager {
public void createProduct(@Valid Product product) {
}
}
Product.java: Product.java:
public class Product {
private int id;
@NotNull
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Tester.java: Tester.java:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/META-INF/applicationContext.xml"})
public class Tester {
@Autowired
ProductManager productManager;
@Test
public void testCreateProduct() {
Product p = new Product();
try {
productManager.createProduct(p);
} catch (Exception e) {
e.printStackTrace();
}
}
}
applicationContext.xml: applicationContext.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.gammay.example" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<qualifier value="validator"/>
</bean>
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>
</beans>
Exception printed in Tester.java: Tester.java中打印的异常:
org.hibernate.validator.method.MethodConstraintViolationException: The following constraint violations occurred: [MethodConstraintViolationImpl [method=public void com.gammay.example.core.ProductManager.createProduct(com.gammay.example.model.Product), parameterIndex=0, parameterName=arg0, kind=PARAMETER, message=may not be null, messageTemplate={javax.validation.constraints.NotNull.message}, rootBean=com.gammay.example.core.ProductManager@12e79d0, rootBeanClass=class com.gammay.example.core.ProductManager, leafBean=com.gammay.example.model.Product@92acdc, invalidValue=null, propertyPath=ProductManager#createProduct(arg0).name, constraintDescriptor=ConstraintDescriptorImpl{annotation=javax.validation.constraints.NotNull, payloads=[], hasComposingConstraints=true, isReportAsSingleInvalidConstraint=false, elementType=FIELD, definedOn=DEFINED_LOCALLY, groups=[interface javax.validation.groups.Default], attributes={message={javax.validation.constraints.NotNull.message}, payload=[Ljava.lang. org.hibernate.validator.method.MethodConstraintViolationException:发生以下约束违规:[MethodConstraintViolationImpl [method = public void com.gammay.example.core.ProductManager.createProduct(com.gammay.example.model.Product),parameterIndex = 0, parameterName = arg0,kind = PARAMETER,message = may not null,messageTemplate = {javax.validation.constraints.NotNull.message},rootBean = com.gammay.example.core.ProductManager @ 12e79d0,rootBeanClass = class com.gammay。 example.core.ProductManager,leafBean = com.gammay.example.model.Product @ 92acdc,invalidValue = null,propertyPath = ProductManager #createProduct(arg0).name,constraintDescriptor = ConstraintDescriptorImpl {annotation = javax.validation.constraints.NotNull,payloads = [],hasComposingConstraints = true,isReportAsSingleInvalidConstraint = false,elementType = FIELD,definedOn = DEFINED_LOCALLY,groups = [interface javax.validation.groups.Default],attributes = {message = {javax.validation.constraints.NotNull.message},有效载荷= [Ljava.lang。 Class;@7ce531, groups=[Ljava.lang.Class;@1ab0086}}]] at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:91) ... 类; @ 7ce531,groups = [Ljava.lang.Class; @ 1ab0086}}]] org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:91)...
If you look at the inner workings of spring's MethodValidationInterceptor
there is a private static class HibernateValidatorDelegate
within buildValidatorFactory. 如果你看一下spring的MethodValidationInterceptor
的内部工作原理, MethodValidationInterceptor
有一个private static class HibernateValidatorDelegate
。 Spring is making the called to configure Hibernate's HibernateValidator
which is doing the actual real validation, hence why it your seeing org.hibernate.validator.method.MethodConstraintViolationException
Spring正在调用配置Hibernate的HibernateValidator
,它正在进行实际的实际验证,因此为什么你会看到org.hibernate.validator.method.MethodConstraintViolationException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.