首页 > 代码库 > 记录webservice接口日志,输入和输出可以记录再一起
记录webservice接口日志,输入和输出可以记录再一起
配置文件配置
<?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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。