Java 在使用Apache CXF发送FastInfo设置请求时,日志中是否可以包含XML?
我需要在应用程序的日志中包含请求和响应,但是Apache CXF发送的请求在FastInfo集中(内容类型:application/FastInfo),这导致请求和响应的日志无法读取(因为它是二进制的)。有没有办法让我保留FastInfo消息(出于性能原因),但在日志中获得适当的XML 下面是我现在拥有的CXF配置,如果有帮助的话:Java 在使用Apache CXF发送FastInfo设置请求时,日志中是否可以包含XML?,java,web-services,cxf,jax-ws,fastinfoset,Java,Web Services,Cxf,Jax Ws,Fastinfoset,我需要在应用程序的日志中包含请求和响应,但是Apache CXF发送的请求在FastInfo集中(内容类型:application/FastInfo),这导致请求和响应的日志无法读取(因为它是二进制的)。有没有办法让我保留FastInfo消息(出于性能原因),但在日志中获得适当的XML 下面是我现在拥有的CXF配置,如果有帮助的话: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="logInbound" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean id="logOutbound" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
<cxf:bus>
<cxf:inInterceptors>
<ref bean="logInbound" />
</cxf:inInterceptors>
<cxf:outInterceptors>
<ref bean="logOutbound" />
</cxf:outInterceptors>
<cxf:outFaultInterceptors>
<ref bean="logOutbound" />
</cxf:outFaultInterceptors>
<cxf:inFaultInterceptors>
<ref bean="logInbound" />
</cxf:inFaultInterceptors>
</cxf:bus>
</beans>
提前感谢您的帮助。我查看了一下,它似乎不支持FastInfo设置。但是您可以使用自定义而不是loggininterceptor
和logginoutinterceptor
来提取有效负载、解码并记录原始消息
public class CustomInterceptor extends AbstractPhaseInterceptor<Message> {
public CustomInterceptor () {
super(Phase.RECEIVE);
}
public void handleMessage(Message message) {
//Get the message body into payload[] and set a new non-consumed inputStream into Message
InputStream in = message.getContent(InputStream.class);
byte payload[] = IOUtils.readBytesFromStream(in);
ByteArrayInputStream bin = new ByteArrayInputStream(payload);
message.setContent(InputStream.class, bin);
//Decode from FastInfoset and write the payload in your preferred log system
OutputStream out = System.out
decodeFI(in,out);
}
public void handleFault(Message messageParam) {
//Invoked when interceptor fails
//Exception e = message.getContent(Exception.class);
}
}
非常感谢,这看起来很棒,我们将测试它。很遗憾,我没有时间测试它,我会验证我什么时候可以测试,但我确实给了你赏金,因为这是一个很好的回应。
<bean id="logInbound" class="test.CustomInterceptor" />
<bean id="logOutbound" class="test.CustomInterceptor" />
public void decodeFI(InputStream in, OutputStream out) throws Exception{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
DOMDocumentParser parser = new DOMDocumentParser();
parser.parse(doc, in);
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
transformer.transform(source, result);
}