記錄webservice介面日誌,輸入和輸出可以記錄再一起

來源:互聯網
上載者:User

標籤: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介面日誌,輸入和輸出可以記錄再一起

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.