webservice總結之cxf

來源:互聯網
上載者:User

標籤:

java webservice有很多種實現方式,處理方式一般有兩種:

  a:基於SOAP的 JAX-WS(Java API for XML-Based Web Services) 比如:RI,Axis,CXF

  b:基於REST的JAX-RS 比如Jersey,RESTEasy,CXF

可以看到CXF 不僅用於開發基於 SOAP 的 WS,也開發於開發基於 REST 的 WS,那就來總結一下CXF的具體用法吧。

SOAP 風格寫法

1,定義介面

import javax.jws.WebService;@WebServicepublic interface HelloFacade {    String sayHello(String msg);}

 

2,服務端實現介面與發布

 a, 實現介面:

import javax.jws.WebService;import org.springframework.stereotype.Component;import com.bresume.core.ws.HelloFacade;@WebService@Componentpublic class HelloFacadeImpl implements HelloFacade{    @Override    public String sayHello(String msg) {        System.out.println("msg="+msg);        return msg+"_success";    }}
HelloFacadeImpl

 b, web.xml配置:

    <!-- CXF -->    <servlet>        <servlet-name>cxf</servlet-name>        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>cxf</servlet-name>        <url-pattern>/ws/*</url-pattern>    </servlet-mapping>
web.xml

 c,spring配置:

  最好將ws設定檔單獨放置,引入

 <import resource="spring-cxf.xml"/>

  建立spring-cxf.xml   jaxws:server 或者 jaxws:endpoint 兩種配置方式都可以

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:jaxws="http://cxf.apache.org/jaxws"    xsi:schemaLocation="http://www.springframework.org/schema/beans                          http://www.springframework.org/schema/beans/spring-beans-3.2.xsd                          http://www.springframework.org/schema/context                          http://www.springframework.org/schema/context/spring-context-3.2.xsd                          http://cxf.apache.org/jaxws                           http://cxf.apache.org/schemas/jaxws.xsd">    <!-- <jaxws:server id="helloFacade" address="/soap/hello">        <jaxws:serviceBean>            <ref bean="helloFacadeImpl" />        </jaxws:serviceBean>    </jaxws:server> -->  <jaxws:endpoint id="helloFacade" implementor="#helloFacadeImpl" address="/soap/hello"/></beans>

服務端配置完畢,訪問wsdl,可看到如下內容,則表示服務端配置成功

3,用戶端訪問

import org.apache.cxf.endpoint.Client;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import com.bresume.core.ws.HelloFacade;public class Test {    static final String url = "http://localhost:8081/ws/soap/hello";    public static void main(String[] args) {        testJaxWsClient();        //testDynamicClient();    }    public static void testJaxWsClient () {        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();        factory.setAddress(url);        factory.setServiceClass(HelloFacade.class);        HelloFacade hellofacade = factory.create(HelloFacade.class);        String result = hellofacade.sayHello("word");        System.out.println("testJaxWsClient"+result);    }    public static void testDynamicClient() {        JaxWsDynamicClientFactory  factory = JaxWsDynamicClientFactory .newInstance();        Client client = factory.createClient("http://localhost:8081/ws/soap/hello?wsdl");        try {            Object[] results = client.invoke("sayHello", "world");            System.out.println("DynamicClientFactory"+results[0]);        } catch (Exception e) {            e.printStackTrace();        }    }}
Test

 -----------未完------

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.