WebService學習筆記-CXF添加攔截器

來源:互聯網
上載者:User

標籤: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添加攔截器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.