zoukankan      html  css  js  c++  java
  • 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>  

  • 相关阅读:
    又来项目了,星座运势widget
    ubuntu下编译android源代码
    Android UI,界面辅助设置工具,可随意拖动控件,比google官方提供的方便
    Android 1.5原生软件开发SDK公布
    REST转自WIKI
    Android SDK 1.5 包装索引
    android google market FreshFace上线了,大家都试用试用,反正免费的
    JSON
    ubuntu命令
    网站支付宝接口错误代码:TRADE_DATA_MATCH_ERROR怎么处理? uz
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317893.html
Copyright © 2011-2022 走看看