今天開始講下攔截器,前面你們學過servlet,struts2 都有攔截器概念,主要做用是作一些權限過濾,編碼處理等;html
webservice也能夠加上攔截器,咱們能夠給webservice請求加權限判斷功能;java
webservice分服務端和客戶端,服務端和客戶端都是能夠加攔截器的,不管是服務端仍是客戶端,都分進,出(In,Out)攔截器;程序員
咱們先來改下服務端的Server類:web
package com.wishwzp.webservice; import javax.xml.ws.Endpoint; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.wishwzp.webservice.impl.HelloWorldImpl; public class Server { public static void main(String[] args) { System.out.println("web service start"); HelloWorld implementor = new HelloWorldImpl(); String address = "http://192.168.0.110/helloWorld"; //Endpoint.publish(address, implementor); // JDK實現 暴露webservice接口 JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean(); factoryBean.setAddress(address); // 設置暴露地址 factoryBean.setServiceClass(HelloWorld.class); // 接口類 factoryBean.setServiceBean(implementor); // 設置實現類 factoryBean.getInInterceptors().add(new LoggingInInterceptor()); // 添加in攔截器 日誌攔截器 factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); // 添加out攔截器 factoryBean.create(); System.out.println("web service started"); } }
這裏的話,咱們經過factoryBean對象能夠獲取攔截器組,添加進或者出攔截器,這裏有個經典的攔截器,咱們開發的時候常常用,就是日誌攔截器,apache
咱們能夠把客戶端的請求,以及服務端返回的信息打印出來,能夠打印控制檯,也能夠打印到執行文件;這裏爲了演示方便,直接搞無參的攔截器,架構
打印到控制檯; frontend
不管是自定義的攔截器,仍是CXF自帶的攔截器,都必須實現Interceptor接口。svn
咱們執行下Server類:測試
而後咱們再來執行下客戶端的Client類,結果:ui
而後咱們仔細觀察Server端的控制檯:
web service start 十一月 06, 2018 5:20:15 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://webservice.wishwzp.com/}HelloWorldService from class com.wishwzp.webservice.HelloWorld 十一月 06, 2018 5:20:16 下午 org.apache.cxf.endpoint.ServerImpl initDestination 信息: Setting the server's publish address to be http://192.168.0.110:80/helloWorld SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. web service started 十一月 06, 2018 5:20:50 下午 org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld 信息: Inbound Message ---------------------------- ID: 1 Address: http://192.168.0.110/helloWorld?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.0.110], User-Agent=[Java/1.8.0_151]} -------------------------------------- 十一月 06, 2018 5:20:51 下午 org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld 信息: Inbound Message ---------------------------- ID: 2 Address: http://192.168.0.110/helloWorld Encoding: UTF-8 Http-Method: POST Content-Type: text/xml; charset=UTF-8 Headers: {Accept=[text/xml, multipart/related], connection=[keep-alive], Content-Length=[188], content-type=[text/xml; charset=UTF-8], Host=[192.168.0.110], SOAPAction=[""], User-Agent=[JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e]} Payload: <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:getRoles xmlns:ns2="http://webservice.wishwzp.com/"></ns2:getRoles></S:Body></S:Envelope> -------------------------------------- 十一月 06, 2018 5:20:51 下午 org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld 信息: Outbound Message --------------------------- ID: 2 Response-Code: 200 Encoding: UTF-8 Content-Type: text/xml Headers: {} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getRolesResponse xmlns:ns2="http://webservice.wishwzp.com/"><return><item><key>wishwzp</key><value><id>1</id><roleName>技術總監</roleName></value><value><id>2</id><roleName>架構師</roleName></value></item><item><key>jack</key><value><id>1</id><roleName>程序員</roleName></value></item></return></ns2:getRolesResponse></soap:Body></soap:Envelope> --------------------------------------
這裏的打印出來的就是日誌信息:Inbound 進信息 Outbound 是出信息,進的時候,你們會看到有個Headers SOAP消息。後面咱們能夠在裏面加咱們的數據;
在Outbound Message裏,Payload消息裏咱們能夠找到webservice返回的數據 SOAP消息;
同理,咱們在客戶端也能夠加進出攔截器,修改Client代碼:
package com.wishwzp.webservice; import java.util.List; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; public class Client { public static void main(String[] args) { HelloWorldService service=new HelloWorldService(); HelloWorld helloWorld=service.getHelloWorldPort(); org.apache.cxf.endpoint.Client client=ClientProxy.getClient(helloWorld); client.getInInterceptors().add(new LoggingInInterceptor()); // 添加in攔截器 日誌攔截器 client.getOutInterceptors().add(new LoggingOutInterceptor()); // 添加out攔截器 //System.out.println(helloWorld.say("wishwzp測試")); // User user=new User(); // user.setUserName("jack"); // user.setPassword("123456"); // List<Role> roleList=helloWorld.getRoleByUser(user); // for(Role role:roleList){ // System.out.println(role.getId()+","+role.getRoleName()); // } MyRoleArray array=helloWorld.getRoles(); List<MyRole> roleList=array.item; for(int i=0;i<roleList.size();i++){ MyRole my=roleList.get(i); System.out.print(my.key+":"); for(Role r:my.value){ System.out.print(r.getId()+","+r.getRoleName()+" "); } System.out.println(); } } }
這裏的話,咱們用到了ClientProxy,客戶端代理。因此也是須要導入對應cxfjar包的,這裏我直接將pox.xml的複製到client項目中
<dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.1.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.5</version> </dependency> </dependencies>
咱們請求的時候,能夠看到控制檯的日誌信息:
十一月 06, 2018 5:28:44 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL 信息: Creating Service {http://webservice.wishwzp.com/}HelloWorldService from WSDL: http://192.168.0.110/helloWorld?wsdl 十一月 06, 2018 5:28:46 下午 org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld 信息: Outbound Message --------------------------- ID: 1 Address: http://192.168.0.110/helloWorld Encoding: UTF-8 Http-Method: POST Content-Type: text/xml Headers: {Accept=[*/*], SOAPAction=[""]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getRoles xmlns:ns2="http://webservice.wishwzp.com/"/></soap:Body></soap:Envelope> -------------------------------------- 十一月 06, 2018 5:28:46 下午 org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld 信息: Inbound Message ---------------------------- ID: 1 Response-Code: 200 Encoding: UTF-8 Content-Type: text/xml; charset=UTF-8 Headers: {content-type=[text/xml; charset=UTF-8], Date=[Tue, 06 Nov 2018 09:28:46 GMT], Server=[Jetty(9.2.11.v20150529)], transfer-encoding=[chunked]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getRolesResponse xmlns:ns2="http://webservice.wishwzp.com/"><return><item><key>wishwzp</key><value><id>1</id><roleName>技術總監</roleName></value><value><id>2</id><roleName>架構師</roleName></value></item><item><key>jack</key><value><id>1</id><roleName>程序員</roleName></value></item></return></ns2:getRolesResponse></soap:Body></soap:Envelope> -------------------------------------- wishwzp:1,技術總監 2,架構師 jack:1,程序員
這裏也是出進的日誌信息,和上面那個相似;
OK到這裏的話,添加攔截器已經講完了;