標籤:webservice
Webservice攔截器:在webservice請求過程中,動態操作請求和響應的資料
分類
按照所處的位置分:伺服器端攔截器 用戶端攔截器
按照訊息的方向分:入攔截器 出攔截器
按照定義者分:系統攔截器 自訂攔截器
在伺服器端添加攔截器
package com.demo;//注意引入的類一定要正確import javax.xml.ws.Endpoint;import org.apache.cxf.interceptor.LoggingInInterceptor;import org.apache.cxf.jaxws.EndpointImpl;public class webServiceApp {public static void main(String[] args) {System.out.println("Starting web service... ");HelloWorldImpl implementor = new HelloWorldImpl();String address = "http://localhost:8080/helloWorld";Endpoint endpoint = Endpoint.publish(address, implementor);// jaxws API 轉到 cxf API 添加日誌攔截器EndpointImpl jaxwsEndpointImpl = (EndpointImpl) endpoint;org.apache.cxf.endpoint.Server server = jaxwsEndpointImpl.getServer();org.apache.cxf.endpoint.Endpoint cxfEndpoint = server.getEndpoint();LoggingInInterceptor logging = new LoggingInInterceptor();cxfEndpoint.getInInterceptors().add(logging);System.out.println("Web service started");}}
用戶端
package com.demo.client;import org.apache.cxf.interceptor.LoggingOutInterceptor;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import com.demo.HelloWorld;import com.demo.User;//參考http://blog.csdn.net/fhd001/article/details/5778915public class HelloWorldClient {public static void main(String[] args) {JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();svr.setServiceClass(HelloWorld.class);svr.setAddress("http://localhost:8080/helloWorld");HelloWorld hw = (HelloWorld) svr.create();// jaxws API 轉到 cxf API 添加日誌攔截器org.apache.cxf.endpoint.Client client = org.apache.cxf.frontend.ClientProxy.getClient(hw);org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();LoggingOutInterceptor logging = new LoggingOutInterceptor();cxfEndpoint.getOutInterceptors().add(logging);User user = new User();user.setUsername("Umgsai");user.setDescription("test");System.out.println(hw.sayHiToUser(user));String sayHi = hw.sayHi("test~~~");System.out.println(sayHi);}}
本文出自 “阿凡達” 部落格,請務必保留此出處http://shamrock.blog.51cto.com/2079212/1563337
WebService學習筆記-CXF添加攔截器