使用CXF拦截器进行错误处理-更改响应消息 [英] Error handling with CXF interceptors - changing the response message
问题描述
我正在尝试处理来自后端的错误.如果发生错误,但内容是XmlMessage的实例,则调用 handleMessage()
.我想将其更改为我自己的响应-只需设置响应代码并添加一些消息即可.
I'm trying to handle errors coming from my backend. The handleMessage()
is called if an error occurs but the content is an instance of XmlMessage. I would like to change it to my own response - just set the response code and add some message.
我没有找到任何合适的文档可以告诉我如何做...
I haven't found any proper documentation which could tell me how to do this...
这些示例是针对REST的,但我也想在SOAP中进行管理.
These axamples are for REST but I'd like to manage this thing in SOAP too.
拦截器
public class ErrorHandlerInterceptor extends AbstractPhaseInterceptor<Message> {
public ErrorHandlerInterceptor() {
super(Phase.POST_LOGICAL);
}
@Override
public void handleMessage(Message message) throws Fault {
Response response = Response
.status(Response.Status.BAD_REQUEST)
.entity("HOW TO GET A MESSAGE FROM AN EXCEPTION IN HERE???")
.build();
message.getExchange().put(Response.class, response);
}
}
context.xml
<bean id="errorHandlerInterceptor"
class="cz.cvut.fit.wst.server.interceptor.ErrorHandlerInterceptor" />
<jaxrs:server address="/rest/">
<jaxrs:serviceBeans>
<ref bean="restService" />
</jaxrs:serviceBeans>
<jaxrs:outFaultInterceptors>
<ref bean="errorHandlerInterceptor" />
</jaxrs:outFaultInterceptors>
</jaxrs:server>
推荐答案
这是难题的另一部分.您已经在使用 JAX-RS ,所以为什么不使用
And here's the other piece of your puzzle. You're already using JAX-RS, so why not use JAX-WS as well?
此线程和
This thread and this blog post cover mapping Exceptions into SOAP faults. Short and sweet:
JAX-WS 2.0规范要求以
@WebFault
必须具有两个构造函数和一个方法[获取故障信息的获取器]:
The JAX-WS 2.0 specification demands that the exception annotated with
@WebFault
must have two constructors and one method [getter to obtain the fault information]:
WrapperException(String message, FaultBean faultInfo)
WrapperException(String message, FaultBean faultInfo, Throwable cause)
FaultBean getFaultInfo()
WrapperException被异常的名称替换,而FaultBean被实现故障bean的类名称替换.故障bean是包含故障信息的Java bean,Web服务客户端使用它来了解故障原因.
The WrapperException is replaced by the name of the exception, and FaultBean is replaced by the class name that implements the fault bean. The fault bean is a Java bean that contains the information of the fault and is used by the Web service client to know the cause for the fault.
有您的映射.只需在 @WebFault
的上下文中指定上述签名的实现,您的SOAP API就应该愉快地映射这些签名.显然,这些链接包含更多详细信息.
And there's your mapping. Simply specify implementations of the above signatures in the context of @WebFault
and your SOAP API should map these happily. Obviously, the links contain more details.
这篇关于使用CXF拦截器进行错误处理-更改响应消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!