gpt4 book ai didi

spring - JAX-RS Bean 验证的自定义异常映射

转载 作者:行者123 更新时间:2023-12-01 16:05:02 27 4
gpt4 key购买 nike

我正在使用 Apache CXF、Spring 和 JAX-RS 构建 REST Web 服务,当输入 JSON 验证失败时,我需要在其中发送自定义异常。

我没有使用业务逻辑,而是尝试使用 CXF、JAX-RS 的开箱即用 bean 验证功能。

Bean 验证工作正常,但是,它总是抛出 500 异常,这不太有用。根据文档,这是 org.apache.cxf.jaxrs.validation.ValidationExceptionMapper 的预期行为。

我正在尝试扩展异常映射器,以便我可以抛出不同的错误消息,但是这个自定义映射器没有被调用。

下面是我的代码设置->

pom.xml

    <!-- Spring Framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>

<!-- CXF -->

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.0.2</version>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.2</version>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.9.3</version>
</dependency>

<!-- Hibernate validation -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
<!-- JSR 303 Validation framework -->
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.19</version>
</dependency>

JAX-RS 服务器
<jaxrs:server address="/" id="iamService">
<jaxrs:serviceBeans>
<bean class="com.XXXXXX.iam.service.core.endpoint.impl.IAMIdentityServiceImpl" />
</jaxrs:serviceBeans>
<jaxrs:inInterceptors>
<ref bean="validationInInterceptor" />
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<ref bean="validationOutInterceptor" />
<ref bean="exceptionMapperOutInterceptor" />
</jaxrs:outInterceptors>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
</jaxrs:extensionMappings>
<jaxrs:providers>
<ref bean='jsonProvider' />
<ref bean="ampfExceptionMapper"/>
</jaxrs:providers>
</jaxrs:server>

<bean id="ampfExceptionMapper" class="com.xxxxxxx.iam.service.core.interceptor.AmpfValidationExceptionMapper"/>
<bean id="exceptionMapperOutInterceptor" class="org.apache.cxf.jaxrs.interceptor.JAXRSOutExceptionMapperInterceptor"/>

<bean id="validationProvider" class="org.apache.cxf.validation.BeanValidationProvider" />

<bean id="validationInInterceptor" class="org.apache.cxf.jaxrs.validation.JAXRSBeanValidationInInterceptor">
<property name="provider" ref="validationProvider" />

</bean>


<bean id="validationOutInterceptor" class="org.apache.cxf.jaxrs.validation.JAXRSBeanValidationOutInterceptor">
<property name="provider" ref="validationProvider" />
</bean>

自定义异常映射器
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class AmpfValidationExceptionMapper implements ExceptionMapper<ConstraintViolationException>{



@Override
public Response toResponse(ConstraintViolationException exception) {
System.out.println("Executing Ameriprise Service exception mapping");

UniqueIDValidationResponse response = new UniqueIDValidationResponse();
response.setResponse("Invalid request");
return Response.ok(response).build();
}


}

bean 类

公共(public)类 UniqueIDValidationRequest {
/*@Autowired
private Validator validator;*/

@XmlAttribute(name = "ssoId")
@NotBlank
@NotEmpty
@Length(max=20,min=5)
protected String ssoId;

@NotEmpty
@NotBlank
@XmlAttribute(name = "epHashId")
protected String epHashId;

最佳答案

这个问题现在已经解决了。我必须做的是创建一个自定义拦截器。这将捕获 bean 验证时发生的错误并添加实际的 bean 验证错误。请检查下面的 InInterceptor 代码 -->

@Provider
public class IamServInInterceptor extends
BeanValidationInInterceptor implements ContainerRequestFilter{

private static Logger logger = LoggerFactory.getLogger(IamServInInterceptor.class);

public IamServInInterceptor() {
logger.debug("IamServInInterceptor default consturctor called");
}

public IamServInInterceptor(String phase) {
super(phase);
logger.debug("IamServInInterceptor phase : " + phase);
}

@Override
public void filter(ContainerRequestContext arg0) throws IOException {
// TODO Auto-generated method stub
logger.debug("IamServInInterceptor filter is called");
InterceptorChain chain = PhaseInterceptorChain.getCurrentMessage()
.getInterceptorChain();
chain.add(this);
}

protected Object getServiceObject(Message message) {
logger.debug("IamServInInterceptor getServiceObject called");
return ValidationUtils.getResourceInstance(message);
}

protected void handleValidation(Message message, Object resourceInstance,
Method method, List<Object> arguments){
try {
logger.debug("IamServInInterceptor handleValidation called");
super.handleValidation(message, resourceInstance, method, arguments);
} catch (ConstraintViolationException ex) {
logger.debug("IamServInInterceptor handleValidation has some exception");
UniqueIDValidationResponse response = new UniqueIDValidationResponse();
response.setResponse("INPUT_VALIDATION_FAILED");
ConstraintViolationException constraintException = (ConstraintViolationException)ex;
Set<ConstraintViolation<?>> constraint=
constraintException.getConstraintViolations();
Iterator<ConstraintViolation<?>> iterator = constraint.iterator();
while(iterator.hasNext()){
ConstraintViolation<?> error = iterator.next();
logger.debug("Error : " + error.toString());
response.getMessage().add(error.getMessage());
}
ex.printStackTrace();
message.getExchange().put(Response.class, Response.ok(response).build());
}
}

}

关于spring - JAX-RS Bean 验证的自定义异常映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31483939/

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