码迷,mamicode.com
首页 > Web开发 > 详细

记录webservice接口日志,输入和输出可以记录再一起

时间:2017-04-18 20:55:47      阅读:399      评论:0      收藏:0      [点我收藏+]

标签:puts   name   etc   tco   pack   geo   beans   blog   oca   

配置文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <context:component-scan base-package="com.test.inf" />

    <jaxws:endpoint id="syncUserService"
        implementor="com.test.inf.testServiceImpl " address="/testInf">
        <jaxws:inInterceptors>
            <bean name="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
            <bean name="webLogInInterceptor" class="com.test.inf.WebLogInInterceptor" />
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
            <bean name="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
            <bean name="webLogOutInterceptor" class="com.test.inf.WebLogOutInterceptor" />
        </jaxws:outInterceptors>
    </jaxws:endpoint>
    
</beans>
WebLogInInterceptor 为输入拦截器
WebLogOutInterceptor 为输出拦截器

package com.test.inf;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;


public class WebLogInInterceptor extends AbstractPhaseInterceptor<Message> {

    public MessageInInterceptor() {
        super(Phase.RECEIVE);
    }

    public void handleMessage(Message message) throws Fault {
        
        
         try {
             String xml;
             InputStream is = message.getContent(InputStream.class);
             
             String encoding = (String)message.get(Message.ENCODING);
             xml = IOUtils.toString(is);
            
             System.out.println("输入报文为:" + xml);
             message.setContent(InputStream.class, new ByteArrayInputStream(xml.getBytes(encoding)));
             message.getExchange().put("idtest", xml);
         } catch (Exception e) {
             e.printStackTrace();
         } 
    }
}

message.setExchage().put("idtest", xml); 将输入信息存储

 

package com.test.inf;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

public class MessageOutInterceptor extends AbstractPhaseInterceptor<Message> {
    @SuppressWarnings("unused")
    private String mMessage;
    
    public MessageOutInterceptor() {
        super(Phase.PRE_STREAM);
    }

   public void handleMessage(Message message) throws Fault {
       String xml;
         try {
            
             String inputXML = (String) message.getExchange().get("idtest"); 
             System.out.println("输入信息::::"+inputXML);
             OutputStream os = message.getContent(OutputStream.class); 
             CachedStream cs = new CachedStream();
             message.setContent(OutputStream.class, cs);
             message.getInterceptorChain().doIntercept(message);
             CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);
             InputStream in = csnew.getInputStream();
             xml = IOUtils.toString(in);
             System.out.println("输出信息:" + xml);
             IOUtils.copy(new ByteArrayInputStream(xml.getBytes()), os); 
             cs.close();
             os.flush();
             message.setContent(OutputStream.class, os); 
            
         } catch (Exception e) {
             e.printStackTrace();
         }
    }
   
  
}

String inputXML = (String) message.getExchange().get("idtest");  获取输入信息 

记录webservice接口日志,输入和输出可以记录再一起

标签:puts   name   etc   tco   pack   geo   beans   blog   oca   

原文地址:http://www.cnblogs.com/lyhappy/p/6729876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!