【问题标题】:Apache CXF :- How do i extract the payload data using cxf interceptorsApache CXF :- 如何使用 cxf 拦截器提取有效负载数据
【发布时间】:2016-07-26 09:37:50
【问题描述】:

我应该遵循哪些步骤来使用 Apache CXF 拦截器提取有效负载?

【问题讨论】:

    标签: java apache web-services cxf interceptor


    【解决方案1】:

    您的拦截器需要从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

    【讨论】:

    • @pedrofb 我在handleMessage方法中写什么代码来获取payload...我是cxf的新手..
    • 看看CXF的源代码LoggingInInterceptor.handleMessageLoggingOutInterceptor.handleMessage。这些类允许在日志中打印完整的消息。您可以从那里复制和粘贴grepcode.com/file/repo1.maven.org/maven2/org.apache.cxf/…
    • @ pedrofb 我已经使用 LoggingInInterceptor 和 LoggingOutInterceptor 在日志中打印了完整的消息。当最终用户发送数据时,我想在 java 类中提取该有效负载......对不起如果我在这里遗漏了什么..
    • 邮件正文可以通过这种方式获得InputStream in = message.getContent(InputStream.class); 在我提供的链接中,如果需要,您可以查看示例以获取其他字段(标题等)。您想在客户端还是在服务器端获取有效负载?
    • 好的,那么代码是正确的。我已经添加了必要的代码来获取有效负载。
    【解决方案2】:

    您可以使用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);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-02
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 2023-04-07
      • 2016-12-08
      • 1970-01-01
      相关资源
      最近更新 更多