首页 > 代码库 > webservice拦截器
webservice拦截器
CXF的拦截器
理解
- 为什么设计拦截器?
- 为了在webservice请求过程中,能动态操作请求和响应数据, CXF设计了拦截器.
- 拦截器分类:
- 按所处的位置分:服务器端拦截器,客户端拦截器
- 按消息的方向分:入拦截器,出拦截器
- 按定义者分:系统拦截器,自定义拦截器
基于jdk的webservice没有拦截器的功能实现。
接口方法:
import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic interface HelloInt { @WebMethod public String sayHello(String name );}
实现:
import javax.jws.WebService;@WebServicepublic 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: 8Address: http://192.168.1.64:8989/d01ws/hello?wsdlHttp-Method: GETContent-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: 9Address: http://192.168.1.64:8989/d01ws/hello?wsdlHttp-Method: GETContent-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: 11Address: http://192.168.1.64:8989/d01ws/helloEncoding: UTF-8Http-Method: POSTContent-Type: text/xml; charset=UTF-8Headers: {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: 1Address: http://192.168.1.64:8989/d01ws/hello?wsdlEncoding: UTF-8Http-Method: GETContent-Type: text/xmlHeaders: {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: 2Address: http://192.168.1.64:8989/d01ws/helloEncoding: UTF-8Http-Method: POSTContent-Type: text/xml; charset=UTF-8Headers: {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?wsdl2014-12-31 22:27:16 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt信息: Inbound Message----------------------------ID: 1Response-Code: 200Encoding: UTF-8Content-Type: text/xml;charset=UTF-8Headers: {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拦截器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。