添加webservice调用日志
之前想用spring的AOP给webservice添加切面的,但是使用around切面后,居然调用端得不到webservice的返回结果,而且报文的详细情况也不得而知,很是尴尬,所以偷了个懒。但是该做的还是要做,不要以后要求查看调用日志的时候,什么都拿不出,不是一个尴尬能搞定的。
我使用的是基于cxf的webservice,所以添加调用日志的方法也是基于cxf的,其次是配合sping开发webservice。基本的webservice的实现,这里就不再说明。
一、使用LoggingInInterceptor实现
第一种方法,就是使用已经的实现的日志拦截,需要配置好log.properties。
<?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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "> <jaxws:endpoint implementor="com.test.DepartServiceImpl" address="/departService">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:endpoint> </beans>
<jaxws:inInterceptors>是输入拦截器,在接口被调用的时候会被拦截。下面是接口被调用后的日志输出。
----------------------------
ID: 1
Address: http://localhost:8080/test/ws/departService?wsdl
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], cache-control=[no-cache], connection=[keep-alive], Content-Length=[221], content-type=[text/xml; charset=UTF-8], host=[localhost:18080], pragma=[no-cache], SOAPAction=[""], user-agent=[Apache CXF 2.7.7]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:findEmployees xmlns:ns2="http://cxf.test.com/"><arg0>张三</arg0><arg1></arg1></ns2:findEmployees></soap:Body></soap:Envelope>
--------------------------------------
<jaxws:outInterceptors>是输出拦截器,在接口调用完毕的时候被拦截。下面是接口调用完成后的日志输出。
---------------------------
ID: 1
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:findEmployeesResponse xmlns:ns2="http://cxf.test.com/"><return><user_depart>IT</user_depart><user_name>张三</user_name><user_state>在职</user_state><user_tele></user_tele><user_type>普通员工</user_type></return></ns2:findEmployeesResponse></soap:Body></soap:Envelope>
--------------------------------------
我们可以看到基本的实现已经非常详细,甚至能看到一些重要基本的信息。如果这个方法还不能满足你的需求,那么请看后面。
二、实现AbstractPhaseInterceptor
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase; import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException; public class WebserviceLogInterceptor extends AbstractPhaseInterceptor<Message> { public WebserviceLogInterceptor(){
super(Phase.RECEIVE);
} @Override
public void handleMessage(Message message) throws Fault {
StringBuffer sb = new StringBuffer();
//这里可以对流做处理,从流中读取数据,然后修改为自己想要的数据
InputStream is = message.getContent(InputStream.class);
String encoding = (String)message.get(Message.ENCODING);
int len = 0;
byte[] bytes = new byte[1024 * 4];
try {
while((len = is.read(bytes))!=-1){
sb.append(new String(bytes, 0, len, encoding));
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(sb.toString());
//在这里需要注意一点的是,如果修改后的soap消息格式不符合webservice框架格式,比如:框架封装后的格式为
//<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" <soap:Body>
//<这里是调用服务端方法的命名空间><这是参数名称>
//这里才是真正的消息
//</这里是调用服务端方法的命名空间></这是参数名称>
//</soap:Body>
//</soap:Envelope>
try {
message.setContent(InputStream.class, new ByteArrayInputStream(sb.toString().getBytes(encoding)));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
配置文件添加对应的拦截器。
<jaxws:endpoint implementor="com.test.cxf.impl.DepartServiceImpl" address="/departService">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean class="com.test.interceptor.WebserviceLogInterceptor" />
</jaxws:inInterceptors>
</jaxws:endpoint>
需要注意的是,在处理完数据后,需要在写回到流中,不然接口无法的到参数。
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; import java.io.*; public class WebserviceLogInterceptor1 extends AbstractPhaseInterceptor<Message> { public WebserviceLogInterceptor1(){
super(Phase.PRE_STREAM);
} @Override
public void handleMessage(Message message) throws Fault {
StringBuffer sb = new StringBuffer();
OutputStream os = message.getContent(OutputStream.class); message.setContent(OutputStream.class, new CachedOutputStream()); message.getInterceptorChain().doIntercept(message); CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);
try {
String encoding = (String)message.get(Message.ENCODING);
InputStream in = csnew.getInputStream(); int len = 0;
byte[] bytes = new byte[1024 * 4];
while((len = in.read(bytes))!=-1) {
sb.append(new String(bytes, 0, len, encoding));
}
System.out.println(sb.toString());
//这里对xml做处理,处理完后同理,写回流中
IOUtils.copy(new ByteArrayInputStream(sb.toString().getBytes(encoding)), os); os.flush(); } catch (IOException e) {
e.printStackTrace();
} message.setContent(OutputStream.class, os);
}
}
同样的,在读取完数据后,仍需要写回到流中,不然调用端得不到结果。
添加webservice调用日志的更多相关文章
- 如何添加WebService调用时的用户认证
场景: 当把发布好的WebService地址或WSDL提供给调用方时,需要对方先进行身份的认证通过后才允许接口的进步访问.而不是公开的谁都可以调用. 解决: 1.在IIS中设置对应网站的目录访问权限. ...
- C#动态webservice调用接口 (JAVA,C#)
C#动态webservice调用接口 using System; using System.Collections; using System.IO; using System.Net; using ...
- 使用自定义签名的https的ssl安全问题解决和metro的webservice调用
最近一直在忙新的项目,每天加班到8点多,都没来写博客了.新的项目遇到了很多问题,现在趁着突然停电来记录下调用https的问题吧. 我们服务主要是,我们调用数据源数据,并且再提供接口供外部数据调用. 我 ...
- WebService 调用
一.WebService调用 1.webservice支持四种调用方式:SOAP 1.1,SOAP 1.2,GET,POST. 2.如果要webservice支持GET,POST调 ...
- WebService调用1(.Net)
1.创建一个最简单的Web Service (1) 新建-项目-ASP.NET空WEB应用程序 (2)添加新项-WEB服务 默认会添加一个HelloWorld方法: using System; us ...
- WebService 调用三种方法
//来源:http://www.cnblogs.com/eagle1986/archive/2012/09/03/2669699.html 最近做一个项目,由于是在别人框架里开发app,导致了很多限制 ...
- webservice调用和生成
webservice简介: Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间 ...
- WebService调用SSAS教程
WebService调用SSAS教程 一.创建SSAS项目 使用SQL Server Business Intelligence Development Studio新建Analysis Servic ...
- 客户端设置WebService调用超时时间
刚接触WebService,对如何在客户端设置WebService调用超时时间查阅了一些资料,现总结如下: ============================================== ...
随机推荐
- [转载]IIS7报500.23错误的解决方法
原文出处: 原文作者:pizibaidu 原文链接:http://pizibaidu.blog.51cto.com/1361909/1794446 背景:今天公司终端上有一个功能打开异常,报500错误 ...
- js判断窗体或容器滚动条到底部
NO1---jquery判断窗体滚动条到底部 $(window).scroll(function () {if ($(window).scrollTop() >= $(document).hei ...
- HTML的doctype和编码
DOCTYPE Doctype是为了告诉浏览器用哪种版本的html去解析指令,必须位于第一行,在某些版本中需要引入DTD,html5无需引入DTD 中文乱码 原因 浏览器解析代码的编码格式与源代码编码 ...
- [转]ASP.NET应用程序生命周期趣谈(四) HttpHandler和页面生命周期
在之前的三篇文章中,我们还算简明扼要的学习了asp.net的整个生命周期,我们知道了一个Request进来以后先去ISAPI Filter,发现是asp.net程序后又ASPNET_ISAPI.dll ...
- android 自定义控件——(四)圆形进度条
----------------------------------↓↓圆形进度条(源代码下有属性解释)↓↓---------------------------------------------- ...
- LinuxMint装JDK和Eclipse
Linux Mint 装JDK和Eclipse 前言 在尝试了好几个发行版后终于锁定了Linux Mint Cinnamon .那么就得配置好环境了. 这里讲一下JAVA环境,配置JDK和Eclips ...
- Centos 安装配置gerrit
关闭selinux,不然nginx的反向代理会报错connect() to 127.0.0.1:8080 failed (13: Permission denied) while connecting ...
- 不重新编译php安装配置eAccelerator
eAccelerator属于一个免费的开源php加速.优化.编译和动态缓存项目,原理和apc类似,都是通过缓存php编译后的opcode代码来提高php脚本的执行性能,而且eAccelerator本身 ...
- python浅谈正则的常用方法
python浅谈正则的常用方法覆盖范围70%以上 上一次很多朋友写文字屏蔽说到要用正则表达,其实不是我不想用(我正则用得不是很多,看过我之前爬虫的都知道,我直接用BeautifulSoup的网页标签去 ...
- 项目自动化建构工具gradle 入门5——在intellij中做一个gradle的web工程
之前的几个小节,都是纯手工建文件夹,纯手工拷贝war包,或者纯手工解压个zip看看结果,,,,这还是我写了玩的helloWorld.若是玩大工程.几十个人的团队协同开发时,这么玩,,,,暴躁的程序员估 ...