1
我应该遵循使用Apache CXF拦截器来提取有效载荷的步骤?Apache CXF: - 如何使用cxf拦截器提取有效载荷数据
我应该遵循使用Apache CXF拦截器来提取有效载荷的步骤?Apache CXF: - 如何使用cxf拦截器提取有效载荷数据
你的拦截器需要从AbstractPhaseInterceptor
或子类来扩展
public class MyInterceptor extends AbstractPhaseInterceptor<Message> {
public MyInterceptor() {
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);
}
public void handleFault(Message messageParam) {
//Invoked when interceptor fails
}
}
添加拦截器编程
MyInterceptor myInterceptor = new MyInterceptor();
Server server = serverFactoryBean.create();
server.getEndpoint().getInInterceptor().add(myInterceptor);
或者使用配置
<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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<bean id="MyInterceptor" class="demo.interceptor.MyInterceptor"/>
<!-- We are adding the interceptors to the bus as we will have only one endpoint/service/bus. -->
<cxf:bus>
<cxf:inInterceptors>
<ref bean="MyInterceptor"/>
</cxf:inInterceptors>
<cxf:outInterceptors>
<ref bean="MyInterceptor"/>
</cxf:outInterceptors>
</cxf:bus>
</beans>
检查文档here
您可以使用LoggingInInterceptor
检索有效载荷为字符串
public class CustomInInterceptor extends LoggingInInterceptor {
/**
* SLF4J Log Instance
*/
private final static Logger LOG = LoggerFactory.getLogger(CustomInInterceptor.class);
/**
* formats logging message and also attaches input xml to thread context
*
* @see org.apache.cxf.interceptor.LoggingInInterceptor#formatLoggingMessage(org.apache.cxf.interceptor.LoggingMessage)
*/
@Override
protected String formatLoggingMessage(final LoggingMessage loggingMessage) {
//The below line reads payload and puts into threads context, which I use later to save in db
KpContextHolder.setInputXml(loggingMessage.getPayload().toString());
return super.formatLoggingMessage(loggingMessage);
}
}
@ pedrofb我该在的handleMessage方法写才能得到有效载荷什么代码...我在CXF .. –
新手看看在CXF'LoggingInInterceptor.handleMessage'和'LoggingOutInterceptor.handleMessage'的源代码中。这些类允许在日志中打印完整的消息。你可以从那里复制和粘贴http://grepcode.com/file/repo1.maven.org/maven2/org.apache.cxf/cxf-core/3.1.1/org/apache/cxf/interceptor/LoggingInInterceptor.java #LoggingInInterceptor.handleMessage%28org.apache.cxf.message.Message%29 – pedrofb
@ pedrofb我已经使用LoggingInInterceptor和LoggingOutInterceptor ....在日志中打印完整的消息。我想在Java类中提取有效负载时用户发送数据....对不起,如果我在这里丢失东西.. –