首页 > Web开发 > 详细

webservice拦截器

时间:2014-12-31 22:47:49      阅读:601      评论:0      收藏:0      [点我收藏+]

CXF的拦截器

 理解

  • 为什么设计拦截器?
  1. 为了在webservice请求过程中,能动态操作请求和响应数据, CXF设计了拦截器.
  • 拦截器分类:
  1. 按所处的位置分:服务器端拦截器,客户端拦截器
  2. 按消息的方向分:入拦截器,出拦截器
  3. 按定义者分:系统拦截器,自定义拦截器

基于jdk的webservice没有拦截器的功能实现。

    接口方法:

   

import javax.jws.WebMethod;
import javax.jws.WebService;


@WebService
public interface HelloInt {
    
    @WebMethod
    public String sayHello(String name );

}

 实现:

import javax.jws.WebService;

@WebService
public class HelloImpl implements HelloInt {

    @Override
    public String sayHello(String name) {
        return "hello,"+name;
    }

}

测试方法:

import javax.xml.ws.Endpoint;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;


public class ServerTest {
    
    public static void main(String[] args) {
        String address = "http://192.168.1.64:8989/d01ws/hello";
        Endpoint point=Endpoint.publish(address,new HelloImpl());
        EndpointImpl impl = (EndpointImpl) point;
        //添加系统日志拦截器
        impl.getInInterceptors().add(new LoggingInInterceptor());
        impl.getInInterceptors().add(new LoggingOutInterceptor());
        System.out.println("webservice发布成功");
    }

}

浏览器中输入发布地址:

2014-12-31 22:07:52 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 8
Address: http://192.168.1.64:8989/d01ws/hello?wsdl
Http-Method: GET
Content-Type: 
Headers: {Accept=[text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2], connection=[keep-alive], Content-Type=[null], Host=[192.168.1.64:8989], User-Agent=[Java/1.6.0_10-rc2]}
--------------------------------------
2014-12-31 22:07:52 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 9
Address: http://192.168.1.64:8989/d01ws/hello?wsdl
Http-Method: GET
Content-Type: 
Headers: {Accept=[text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2], connection=[keep-alive], Content-Type=[null], Host=[192.168.1.64:8989], User-Agent=[Java/1.6.0_10-rc2]}
--------------------------------------


利用eclipse 自带的webservice 浏览器添加请求参数 kity,

2014-12-31 22:09:46 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 11
Address: http://192.168.1.64:8989/d01ws/hello
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[application/soap+xml, application/dime, multipart/related, text/*], Cache-Control=[no-cache], connection=[close], Content-Length=[325], content-type=[text/xml; charset=UTF-8], Host=[192.168.1.64:8989], Pragma=[no-cache], SOAPAction=[""], User-Agent=[IBM Web Services Explorer]}
Payload: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://wa.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <q0:sayHello>
      <arg0>kity</arg0>
    </q0:sayHello>
  </soapenv:Body>
</soapenv:Envelope>

--------------------------------------


另外附上wsdl的文档结构图:

<?xml version=‘1.0‘ encoding=‘UTF-8‘?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://wa.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloImplService" targetNamespace="http://wa.com/">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://wa.com/" elementFormDefault="unqualified" targetNamespace="http://wa.com/" version="1.0">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
  </wsdl:types>
  <wsdl:message name="sayHelloResponse">
    <wsdl:part element="tns:sayHelloResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="sayHello">
    <wsdl:part element="tns:sayHello" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="HelloInt">
    <wsdl:operation name="sayHello">
      <wsdl:input message="tns:sayHello" name="sayHello">
    </wsdl:input>
      <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="HelloImplServiceSoapBinding" type="tns:HelloInt">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="sayHello">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="sayHello">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="sayHelloResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="HelloImplService">
    <wsdl:port binding="tns:HelloImplServiceSoapBinding" name="HelloImplPort">
      <soap:address location="http://192.168.1.64:8989/d01ws/hello"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

 二、客户端调用webservice:

     先用wsdl2java 生成客户端代码:

   

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

import com.wa.HelloImplService;
import com.wa.HelloInt;

/**
 * 客户端进行测试
 *
 */
public class ClientTest {
    
    public static void main(String[] args) {
        
        HelloImplService service = new HelloImplService();
        HelloInt hello = service.getHelloImplPort();
        //客户端拦截器
        Client client =ClientProxy.getClient(hello);
        client.getInInterceptors().add(new LoggingInInterceptor());
        client.getInInterceptors().add(new LoggingOutInterceptor());
        System.out.println(hello.sayHello("what do you say?please say agin."));
    }

}

可以看到:

服务端的日志信息为:

----------------------------
ID: 1
Address: http://192.168.1.64:8989/d01ws/hello?wsdl
Encoding: UTF-8
Http-Method: GET
Content-Type: text/xml
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], content-type=[text/xml], Host=[192.168.1.64:8989], Pragma=[no-cache], User-Agent=[Apache CXF 2.5.9]}
--------------------------------------
2014-12-31 22:27:16 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 2
Address: http://192.168.1.64:8989/d01ws/hello
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[210], content-type=[text/xml; charset=UTF-8], Host=[192.168.1.64:8989], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.5.9]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://wa.com/"><arg0>what do you say?please say agin.</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------

客户端日志信息为:

2014-12-31 22:27:15 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://wa.com/}HelloImplService from WSDL: http://192.168.1.64:8989/d01ws/hello?wsdl
2014-12-31 22:27:16 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[236], content-type=[text/xml;charset=UTF-8], Server=[Jetty(7.5.4.v20111024)]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://wa.com/"><return>hello,what do you say?please say agin.</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------
hello,what do you say?please say agin.

webservice调用完成。

三、自定义拦截器的实现:

webservice拦截器

原文:http://www.cnblogs.com/lonelywolfmoutain/p/4196693.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!