gpt4 book ai didi

java - Spring+Hibernate验证: throws Hibernate Exceptions and not Spring exceptions

转载 作者:搜寻专家 更新时间:2023-11-01 03:38:03 26 4
gpt4 key购买 nike

我有一个简单的项目(详情如下),它有一个 Spring bean 并使用 HV 作为验证提供程序,用于方法参数验证。

问题:当验证失败时,它会抛出 Hibernate 异常(org.hibernate.validator.method.MethodConstraintViolationException)。但我预计它会抛出 Spring 异常 (org.springframework.web.bind.MethodArgumentNotValidException),因为 Spring 是包装接口(interface)。据我所知,我不关心谁是验证实现者,只需要处理 Spring 定义的类。

问题一:上述论点是否正确,是否应该抛出Spring异常?

问题 2:如果 Hibernate 异常是常态,我如何将其映射到友好消息(在 Google 上找不到此信息)

ProductManager.java:

@Component
@Validated
public class ProductManager {

public void createProduct(@Valid Product product) {

}
}

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;
}

}

测试器.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:

<?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>

在 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.Class;@7ce531, groups=[Ljava.lang.Class;@1ab0086}}]] at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:91) ...

最佳答案

如果您查看 spring 的 MethodValidationInterceptor 的内部工作原理,在 buildValidatorFactory 中有一个 private static class HibernateValidatorDelegate。 Spring 正在调用以配置 Hibernate 的 HibernateValidator,它正在执行实际的验证,因此您会看到 org.hibernate.validator.method.MethodConstraintViolationException

关于java - Spring+Hibernate验证: throws Hibernate Exceptions and not Spring exceptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24083727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com