添加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调用超时时间查阅了一些资料,现总结如下: ============================================== ...
随机推荐
- 基于位图(Bitmap、BitmapData)的图片处理方法(C#)
目前操作位图的主流方法有三种: 1.基于Bitmap像素的处理方法,以GetPixel()和SetPixel()方法为主.方法调用简单,但是效率偏低. 2.基于内存的像素操作方法,以System.Ru ...
- 360安全卫士造成Sharepoint文档库”使用资源管理器打开“异常
备注:企业用户还是少用360为妙 有客户反馈:部门里的XP SP2环境客户机全部异常,使用资源管理器打开Sharepoint文档库,看到的界面样式很老土,跟本地文件夹不一样 ...
- app字体被放大效果发虚
IOS App所有字体被放大,显示效果发虚 小小程序猿 我的博客:http://daycoding.com 分析原因: 由于新版本上线更换了LaunchImage,没有注意美工给的图片尺寸,由于图片尺 ...
- Hibernate 系列 04 - Hibernate 配置相关的类
引导目录: Hibernate 系列教程 目录 前言: 通过上一篇的增删改查小练习之后,咱们大概已经掌握了Hibernate的基本用法. 我们发现,在调用Hibernate API的过程中,虽然Hib ...
- 1-web应用之LAMP源码环境搭建
目录 一.LAMP环境的介绍 1.LAMP环境的重要性 2.LAMP组件介绍 二.Apache源码安装 1.下载Apache以及相关依赖包 2.安装Apache以及相关 ...
- 【原】移动web滑屏框架分享
本月26号参加webrebuild深圳站,会上听了彪叔的对初心的讲解,“工匠精神”这个词又一次被提出,也再次引起了我对它的思考.专注一个项目并把它做得好,很好,更好...现实工作中,忙忙碌碌,抱着完成 ...
- 常用算法——排序(二)
简单选择排序法 选择排序(Selection Sort)的基本思想:对n个记录进行扫描,选择最小的记录,将其输出,接着在剩下的n-1个记录中扫描,选择最小的记录将其输出,--不断重复这个过程,直到只剩 ...
- BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 [treap hash]
1862: [Zjoi2006]GameZ游戏排名系统 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1318 Solved: 498[Submit][ ...
- BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]
3529: [Sdoi2014]数表 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1399 Solved: 694[Submit][Status] ...
- Spring 事务详解
实现购买股票案例: 一.引入JAR文件: 二.开始搭建分层架构---创建账户(Account)和股票(Stock)实体类 Account: ? 1 2 3 4 5 6 7 8 9 10 11 12 1 ...