JAX-WS的Handler和Servlet的Filter相似,能够对全部WebServicer进行拦截,在Handler中能够记录日志、权限控制、对请求的SOAP消息进行加密。解密等。JAX-WS提供两个Handler接口,LogicalHandler和SOAPHandler。LogicalHandler处理的是Message Payload。仅仅能够訪问消息单元中的SOAP消息体。SOAPHandler处理的是整个SOAP消息(包括SOAP header和SOAP body),能够訪问整个SOAP消息。

注冊Handler的方式有以下几种:

使用HandlerResolver(client比較方便)

使用HandlerChain注解和配置文件

从WSDL生成

使用Custom Binding声明HandlerChain

实例代码http://download.csdn.net/detail/accountwcx/8922191

JAX-WS中WebService运行顺序如图所看到的

以下用SOAPHandler实如今WebService服务端记录请求内容和响应内容。

import java.io.IOException;
import java.util.Set; import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext; /**
* 记录SOAP请求及响应
* @author accountwcx@qq.com
*
*/
public class LoggerHandler implements SOAPHandler<SOAPMessageContext> { @Override
public void close(MessageContext context) {
} @Override
public boolean handleFault(SOAPMessageContext context) {
return true;
} @Override
public boolean handleMessage(SOAPMessageContext context) {
// 推断消息是输入还是输出
Boolean output = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
System.out.println(output ? "响应SOAP:" : "请求SOAP:"); SOAPMessage message = context.getMessage(); try {
message.writeTo(System.out);
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} System.out.println(""); return true;
} @Override
public Set<QName> getHeaders() {
return null;
} }

在classpath下建handler-chain.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<javaee:handler-chain>
<javaee:handler>
<javaee:handler-class>com.rvho.server.ws.handler.LoggerHandler</javaee:handler-class>
</javaee:handler>
</javaee:handler-chain>
</javaee:handler-chains>

在服务实现类上加入HandlerChain配置

package com.rvho.server.ws.impl;

import java.util.Date;

import javax.jws.HandlerChain;
import javax.jws.WebService; import com.rvho.server.ws.HelloWService; @WebService(
endpointInterface = "com.rvho.server.ws.HelloWService",
portName = "HelloWSPort",
serviceName = "HelloWSService",
targetNamespace = "http://www.tmp.com/ws/hello"
)
@HandlerChain(file="handler-chain.xml") //加入Handler配置文件
public class HelloWServiceImpl implements HelloWService {
public String index() {
return "hello";
} public Integer add(Integer x, Integer y) {
return x + y;
} public Date now() {
return new Date();
}
}

服务实现接口

package com.rvho.server.ws;

import java.util.Date;

import javax.jws.WebService;

/**
* WebService接口
*/
@WebService(
name = "HelloWS",
targetNamespace = "http://www.tmp.com/ws/hello"
)
public interface HelloWService {
/**
* 返回字符串
*
* @return
*/
String index(); /**
* 两个整数相加
*
* @param x
* @param y
* @return 相加后的值
*/
Integer add(Integer x, Integer y); /**
* 返回当前时间
*
* @return
*/
Date now();
}

client发起index请求,服务端的记录

请求SOAP:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:index xmlns:ns2="http://www.tmp.com/ws/hello" />
</S:Body>
</S:Envelope> 响应SOAP:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:indexResponse xmlns:ns2="http://www.tmp.com/ws/hello">
<return>hello</return>
</ns2:indexResponse>
</S:Body>
</S:Envelope>

JAX-WS HandlerChain使用具体解释的更多相关文章

  1. Java 5 、6、 7中新特性

    JDK5新特性(与1.4相比)[转] 1 循环 for (type variable : array){ body} for (type variable : arrayList){body} 而1. ...

  2. Spring 4 集成Apache CXF开发JAX-RS Web Service

    什么是JAX-RS 在JSR-311规范中定义,即Java API for RESTful Web Services,一套Java API,用于开发 RESTful风格的Webservice. 工程概 ...

  3. JAX-WS使用Handler Chain加工消息

    承前 本文的示例,是基于前一篇文章中的实例而改进的,如果想要运行本文的代码例子,需要先实现前一篇的代码. 前一篇文章JAX-WS开发WebService初级 Handler概念 在WebService ...

  4. iOS开发之 几本书

    <object_c 编程之道书> <iOS 7 UI Transition Guide> iOS开发指南:从零基础到App Store上架[国内第一本iOS架构设计图书,涵盖i ...

  5. Ocelot中文文档-Websockets

    Ocelot额外支持代理websockets.这个功能在问题 212中被提出. 为了是Ocelot代理websocket,你需要做如下事情. 在你的Configure方法中,你要告知应用程序使用Web ...

  6. weblogic项目转为tomcat之后出现的问题

    解决java - JAX-WS和版本冲突 itPublisher分享于2017-03-19 推荐:JWS,JAX-WS,JAX-RS,REST,Restlet,SOAP(JAVA Web Servic ...

  7. Web Service-- 使用 JDK 发布 WS

    Web Service,即“Web 服务”,简写为 WS,从字面上理解,它其实就是“基于 Web 的服务”.而服务却是双方的,有服务需求方,就有服务提供方.服务提供方对外发布服务,服务需求方调用服务提 ...

  8. DOS命令大全--具体解释

    在Linux和Windows下都能够用nslookup命令来查询域名的解析结果 DOS命令大全一)MD--建立子文件夹 1.功能:创建新的子文件夹 2.类型:内部命令 3.格式:MD[盘符:][路径名 ...

  9. Java API 各个包的内容解释

    java.applet 提供创建 applet 所必需的类和 applet 用来与其 applet 上下文通信的类. java.awt 包含用于创建用户界面和绘制图形图像的所有类. java.awt. ...

随机推荐

  1. jquery.lazyload.js实现图片懒载入

    个人理解:将须要延迟载入的图片的src属性所有设置为一张同样尽可能小(目的是尽可能的少占宽带,节省流量,因为缓存机制,当浏览器载入了一张图片之后,同样的图片就会在缓存中拿.不会又一次到server上拿 ...

  2. linux开发node相关的工具

    epel-release yum install epel-release node yum install nodejs mongodb 安装mongodb服务器端 yum install mong ...

  3. mormot解析天气预报JSON数据

    mormot解析天气预报JSON数据 uses SynCommons; constjson2 = '{' + #13#10 +'"error":0,' + #13#10 +'&qu ...

  4. Android Binder总结

    1. MediapplayerService 的启动,怎样在ServiceManager注冊的,不解说详细的细节 ServiceManager 是整个系统的Service总管,其余的系统服务都是通过d ...

  5. ubuntu 安装 consul

    $ wget https://releases.hashicorp.com/consul/1.1.0/consul_1.1.0_linux_amd64.zip $ sudo apt-get insta ...

  6. oracle sql 优化大全

    转自: http://panshaobinsb.iteye.com/blog/1718233 http://yulimeander.blog.sohu.com/115850824.html 最近遇到了 ...

  7. android用jsonReader来解析json

    对于这个json: { "id" : "3232", "data" : [{ "data1" : "555&q ...

  8. Web开发中的6个坏习惯

    在 Usersnap,我们在能很好的组织网站开发有超过20(总和)年的经验.我们认为这些过去的经验能让我们很好的分辨出什么是好.坏和丑陋的网站开发.如今我们不想把注意力放在消极的部分,但就这一次,我们 ...

  9. [置顶] 自娱自乐1之Linux UDC驱动(形式模板)

    首先,我不是做驱动的开发人员.所以只能用自娱自乐来表示我的行为. 我不知道udc和gadget驱动是不是冷门的驱动,资料真是不多.我之前买了一本书,上面说到这些,就教你如何调试已写好的驱动.这样也可以 ...

  10. [IDEA]IntelliJ IDEA 报 This license BIG3CLIK6F has been cancelled 错误

    JetBrains 最近封杀了lanyus提供的序列号,用的人多了,自然会引起JetBrains的注意. 在激活时,会先在本地做一次验证,然后会把注册码发送到JetBrains的账号服务器上accou ...