JAX-WS HandlerChain使用详解

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(客户端比较方便)

使用HandlerChain注解和配置文件

从WSDL生成

使用Custom Binding声明HandlerChain


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


JAX-WS中WebService执行顺序如图所示



下面用SOAPHandler实现在WebService服务端记录请求内容和响应内容。

[java]  view plain  copy
  1. import java.io.IOException;  
  2. import java.util.Set;  
  3.   
  4. import javax.xml.namespace.QName;  
  5. import javax.xml.soap.SOAPException;  
  6. import javax.xml.soap.SOAPMessage;  
  7. import javax.xml.ws.handler.MessageContext;  
  8. import javax.xml.ws.handler.soap.SOAPHandler;  
  9. import javax.xml.ws.handler.soap.SOAPMessageContext;  
  10.   
  11. /** 
  12.  * 记录SOAP请求及响应 
  13.  * @author accountwcx@qq.com 
  14.  * 
  15.  */  
  16. public class LoggerHandler implements SOAPHandler<SOAPMessageContext> {  
  17.   
  18.     @Override  
  19.     public void close(MessageContext context) {  
  20.     }  
  21.   
  22.     @Override  
  23.     public boolean handleFault(SOAPMessageContext context) {  
  24.         return true;  
  25.     }  
  26.   
  27.     @Override  
  28.     public boolean handleMessage(SOAPMessageContext context) {  
  29.         // 判断消息是输入还是输出  
  30.         Boolean output = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);  
  31.         System.out.println(output ? "响应SOAP:" : "请求SOAP:");  
  32.           
  33.         SOAPMessage message = context.getMessage();  
  34.           
  35.         try {  
  36.             message.writeTo(System.out);  
  37.         } catch (SOAPException e) {  
  38.             e.printStackTrace();  
  39.         } catch (IOException e) {  
  40.             e.printStackTrace();  
  41.         }  
  42.           
  43.         System.out.println("");  
  44.   
  45.         return true;  
  46.     }  
  47.   
  48.     @Override  
  49.     public Set<QName> getHeaders() {  
  50.         return null;  
  51.     }  
  52.   
  53. }  

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

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  4.     <javaee:handler-chain>  
  5.         <javaee:handler>  
  6.             <javaee:handler-class>com.rvho.server.ws.handler.LoggerHandler</javaee:handler-class>  
  7.         </javaee:handler>  
  8.     </javaee:handler-chain>  
  9. </javaee:handler-chains>  

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

[java]  view plain  copy
  1. package com.rvho.server.ws.impl;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.jws.HandlerChain;  
  6. import javax.jws.WebService;  
  7.   
  8. import com.rvho.server.ws.HelloWService;  
  9.   
  10. @WebService(  
  11.     endpointInterface = "com.rvho.server.ws.HelloWService",  
  12.     portName = "HelloWSPort",  
  13.     serviceName = "HelloWSService",  
  14.     targetNamespace = "http://www.tmp.com/ws/hello"  
  15. )  
  16. @HandlerChain(file="handler-chain.xml"//添加Handler配置文件  
  17. public class HelloWServiceImpl implements HelloWService {  
  18.     public String index() {  
  19.         return "hello";  
  20.     }  
  21.   
  22.     public Integer add(Integer x, Integer y) {  
  23.         return x + y;  
  24.     }  
  25.   
  26.     public Date now() {  
  27.         return new Date();  
  28.     }  
  29. }  

服务实现接口

[java]  view plain  copy
  1. package com.rvho.server.ws;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.jws.WebService;  
  6.   
  7. /** 
  8.  * WebService接口 
  9.  */  
  10. @WebService(  
  11.     name = "HelloWS",  
  12.     targetNamespace = "http://www.tmp.com/ws/hello"  
  13. )  
  14. public interface HelloWService {  
  15.     /** 
  16.      * 返回字符串 
  17.      *  
  18.      * @return 
  19.      */  
  20.     String index();  
  21.   
  22.     /** 
  23.      * 两个整数相加 
  24.      *  
  25.      * @param x 
  26.      * @param y 
  27.      * @return 相加后的值 
  28.      */  
  29.     Integer add(Integer x, Integer y);  
  30.   
  31.     /** 
  32.      * 返回当前时间 
  33.      *  
  34.      * @return 
  35.      */  
  36.     Date now();  
  37. }  

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

[plain]  view plain  copy
  1. 请求SOAP:  
  2. <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"  
  3.             xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">  
  4.     <SOAP-ENV:Header/>  
  5.     <S:Body>  
  6.         <ns2:index xmlns:ns2="http://www.tmp.com/ws/hello" />  
  7.     </S:Body>  
  8. </S:Envelope>  
  9.   
  10. 响应SOAP:  
  11. <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"  
  12.             xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">  
  13.     <SOAP-ENV:Header/>  
  14.     <S:Body>  
  15.         <ns2:indexResponse xmlns:ns2="http://www.tmp.com/ws/hello">  
  16.             <return>hello</return>  
  17.         </ns2:indexResponse>  
  18.     </S:Body>  
  19. </S:Envelope>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值