我写了一个自定义的CXF拦截记录所有的SOAP请求和响应到数据库中,它似乎是工作的罚款与正面测试用例和服务器错误。
但是,当一个SOAP错误发生,它只是忽略我的拦截,并没有什么记录。
自定义CXF拦截器。
public class DbLogOutInterceptor extends AbstractSoapInterceptor {
public void handleMessage(SoapMessage message) {
logger.info("Handling outbound request");
String transaction = MDC.get(mdcKey);
logger.info("OutBound Transaction ID : {} ", transaction);
//code to log the SOAP message in database
.......
}
}
我不是从这个方法,而不是我看到看到日志报表
11:56:34,102 INFO [Soap12FaultOutInterceptor] class org.apache.cxf.binding.soap.interceptor.Soap12FaultOutInterceptor$Soap12FaultOutInterceptor Internalapplication/soap+xml
11:56:34,103 INFO [EligibilityInfo] Outbound Message
---------------------------
ID: 2
Response-Code: 500
Encoding: UTF-8
Content-Type: application/soap+xml
Headers: {}
Payload :
我所拥有的,以捕捉SOAP错误错误回报在我的自定义拦截器做。
所以在我的自定义拦截我写了下面的代码:
Fault fault = new Fault(message.getContent(Exception.class));
现在,这是这是从Java抛出异常,让框架将其转换为一个故障一些遗留代码。 我不会进入我对感情,但是这将让你所产生的故障。
现在,如果你扔从服务方法的错误,做
Fault fault = message.getContect(Fault.class);
希望这将有助于你得到答案,你想要什么。 请确保您注册拦截器像下面
<jaxws:endpoint
id="fooService" implementor="com.bar.FooService" address="/FooServices">
<jaxws:outFaultInterceptors>
<ref class="com.bar.interceptor.DbLogOutInterceptor"/>
</jaxws:outFaultInterceptors>
</jaxws:endpoint>
<jaxws:endpoint
要捕获错误,你需要注册拦截器的拦截器故障。 例如
<cxf:outFaultInterceptors>
<bean class="DbLogOutInterceptor" />
</cxf:outFaultInterceptors>
看到CXF配置用于使用所述CXF测井拦截器为例来捕获输入/输出消息和/出故障“使用自定义CXF bean元素启用消息日志记录”下页。
最好的办法是实行错误监听器,并使用org.slf4j.Logger登录,而不是使用Java记录错误消息。
这将是明智的覆盖LoggingInInterceptor写乌尔custome拦截器来获得请求负载。
public class CxfInputFaultInterceptor extends AbstractLoggingInterceptor {
private static final Logger LOG = LogUtils.getLogger(CxfInputFaultInterceptor.class);
public CxfInputFaultInterceptor() {
super(Phase.RECEIVE);
}
步骤1:CXF-beans.xml文件
<cxf:bus>
<cxf:inInterceptors>
<ref bean="cxfInputFaultInterceptor"/>
</cxf:inInterceptors>
<cxf:inFaultInterceptors>
<ref bean="cxfInputFaultInterceptor"/>
</cxf:inFaultInterceptors>
<cxf:properties>
<entry key="org.apache.cxf.logging.FaultListener">
<bean id="cxfFaultListener" class="pkg.common.ws.interceptor.CxfFaultListenerImpl" >
<property name="loggedUser" ref="loggedUser"/>
</bean>
</entry>
</cxf:properties>
</cxf:bus>
第2步:你的听众应该实现org.apache.cxf.logging.FaultListener
import java.io.InputStream;
import org.apache.cxf.interceptor.LoggingMessage;
import org.apache.cxf.logging.FaultListener;
import org.apache.cxf.message.Message;
import org.apache.cxf.service.model.EndpointInfo;
import org.apache.cxf.service.model.InterfaceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* Listener to faults on the processing of messages by CXF intercepter chain. Here we override
* java.util.logging.Logger and use org.slf4j.Logger to print error to console.
*/
public class CxfFaultListenerImpl implements FaultListener{
private static final Logger logger = LoggerFactory.getLogger(CxfFaultListenerImpl.class);
private static final String NEWLINE="\n";
public boolean faultOccurred(final Exception exception,final String description,final Message message) {
createErrorLog(message);
logger.error(" --------------------------------------------------------------------------------------------------");
logger.error(" Stack Trace : ");
logger.error(" --------------------------------------------------------------------------------------------------");
logger.error(NEWLINE,exception);
logger.error(" --------------------------------------------------------------------------------------------------");
return true;
}
private void createErrorLog(final Message message){
final Message inMessage = message.getExchange().getInMessage();
final InputStream is = inMessage.getContent(InputStream.class);
final EndpointInfo endpoint = message.getExchange().getEndpoint().getEndpointInfo();
String logName=null;
if(endpoint!=null && endpoint.getService()!=null){
final String serviceName = endpoint.getService().getName().getLocalPart();
final InterfaceInfo iface = endpoint.getService().getInterface();
final String portName = endpoint.getName().getLocalPart();
final String portTypeName = iface.getName().getLocalPart();
logName = serviceName + "." + portName + "." + portTypeName;
}
final LoggingMessage buffer
= new LoggingMessage("Error occured on Service Call : "+NEWLINE +"----------------------------", logName);
final Integer responseCode = (Integer)message.get(Message.RESPONSE_CODE);
if (responseCode != null) {
buffer.getResponseCode().append(responseCode);
}
final String encoding = (String)message.get(Message.ENCODING);
if (encoding != null) {
buffer.getEncoding().append(encoding);
}
final String httpMethod = (String)message.get(Message.HTTP_REQUEST_METHOD);
if (httpMethod != null) {
buffer.getHttpMethod().append(httpMethod);
}
final String ct = (String)message.get(Message.CONTENT_TYPE);
if (ct != null) {
buffer.getContentType().append(ct);
}
final Object headers = message.get(Message.PROTOCOL_HEADERS);
if (headers != null) {
buffer.getHeader().append(headers);
}
final String uri = (String)message.get(Message.REQUEST_URL);
if (uri != null) {
buffer.getAddress().append(uri);
final String query = (String)message.get(Message.QUERY_STRING);
if (query != null) {
buffer.getAddress().append("?").append(query);
}
}
final String requestXml= is.toString();
if(requestXml !=null){
buffer.getMessage().append("LoggedIn User: ");
buffer.getMessage().append(getCurrentUsername()+NEWLINE);
buffer.getMessage().append("Request payload : "+NEWLINE);
buffer.getMessage().append(requestXml);
}else{
buffer.getMessage().append("LoggedIn User: ");
buffer.getMessage().append(getCurrentUsername()+NEWLINE);
buffer.getMessage().append("No inbound request message to append.");
}
logger.error(buffer.toString());
}
}
希望可以帮助别人,只有当上的服务电话中发生错误,从而避免巨大的日志文件,谁只是想堆栈跟踪和有效载荷。
文章来源: Catching the SOAP Fault error in custom interceptor (Soap12FaultOutInterceptor)