之前想用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调用日志的更多相关文章

  1. 如何添加WebService调用时的用户认证

    场景: 当把发布好的WebService地址或WSDL提供给调用方时,需要对方先进行身份的认证通过后才允许接口的进步访问.而不是公开的谁都可以调用. 解决: 1.在IIS中设置对应网站的目录访问权限. ...

  2. C#动态webservice调用接口 (JAVA,C#)

    C#动态webservice调用接口 using System; using System.Collections; using System.IO; using System.Net; using ...

  3. 使用自定义签名的https的ssl安全问题解决和metro的webservice调用

    最近一直在忙新的项目,每天加班到8点多,都没来写博客了.新的项目遇到了很多问题,现在趁着突然停电来记录下调用https的问题吧. 我们服务主要是,我们调用数据源数据,并且再提供接口供外部数据调用. 我 ...

  4. WebService 调用

    一.WebService调用 1.webservice支持四种调用方式:SOAP 1.1,SOAP 1.2,GET,POST.           2.如果要webservice支持GET,POST调 ...

  5. WebService调用1(.Net)

    1.创建一个最简单的Web Service (1)  新建-项目-ASP.NET空WEB应用程序 (2)添加新项-WEB服务 默认会添加一个HelloWorld方法: using System; us ...

  6. WebService 调用三种方法

    //来源:http://www.cnblogs.com/eagle1986/archive/2012/09/03/2669699.html 最近做一个项目,由于是在别人框架里开发app,导致了很多限制 ...

  7. webservice调用和生成

    webservice简介: Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间 ...

  8. WebService调用SSAS教程

    WebService调用SSAS教程 一.创建SSAS项目 使用SQL Server Business Intelligence Development Studio新建Analysis Servic ...

  9. 客户端设置WebService调用超时时间

    刚接触WebService,对如何在客户端设置WebService调用超时时间查阅了一些资料,现总结如下: ============================================== ...

随机推荐

  1. 为什么applicationContext.xml和spring-servlet.xml中都有注解过滤<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.controller" />

    在刚学习SpringMVC框架整合时,你也许会产生疑问为什么Spring.xml和SpringMVC.xml中都有注解过滤. <context:component-scan base-packa ...

  2. .NET Core 2.0版本预计于2017年春季发布

    英文原文: NET Core 2.0 Planned for Spring 2017 微软项目经理 Immo Landwerth 公布了即将推出的 .NET Core 2.0 版本的细节,该版本预计于 ...

  3. 每天一个设计模式-2 外观模式(Facade)

    每天一个设计模式-2  外观模式(Facade) 1.生活中的示例 客户想要购买一台电脑,一般有两种方法: 1.自己DIY,客户需要知道组成电脑的所有电子器件,并且需要熟悉那些配件,对客户要求较高. ...

  4. 深入浅出node(2) 模块机制

    这部分主要总结深入浅出Node.js的第二章 一)CommonJs 1.1CommonJs模块定义 二)Node的模块实现 2.1模块分类 2.2 路径分析和文件定位 2.2.1 路径分析 2.2.2 ...

  5. JDBC数据库访问操作的动态监测 之 Log4JDBC

    log4jdbc是一个JDBC驱动器,能够记录SQL日志和SQL执行时间等信息.log4jdbc使用SLF4J(Simple Logging Facade)作为日志系统. 特性: 1.支持JDBC3和 ...

  6. Oracle学习笔记十 使用PL/SQL

    PL/SQL 简介 PL/SQL 是过程语言(Procedural Language)与结构化查询语言(SQL)结合而成的编程语言,是对 SQL 的扩展,它支持多种数据类型,如大对象和集合类型,可使用 ...

  7. redis 缓存技术与memcache的区别

    1 什么是redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset( ...

  8. k近邻算法(knn)的c语言实现

    最近在看knn算法,顺便敲敲代码. knn属于数据挖掘的分类算法.基本思想是在距离空间里,如果一个样本的最接近的k个邻居里,绝大多数属于某个类别,则该样本也属于这个类别.俗话叫,"随大流&q ...

  9. svn web页面管理svnadmin部署

    1.yum install subversion  创建svn目录  mdkir -p /data/svndirroot  建立svn库 svnadmin create /data/svndirroo ...

  10. Linux 文件系统分区基础

    文件系统就是管理设备,组织文件的一些结构和算法. /boot分区,它包含了操作系统的内核和在启动系统过程中所要用到的文件, 建这个分 区是有必要的,因为目前大多数的PC机要受到BIOS的限制,况且如果 ...