WebService Summary of the CXF

Source: Internet
Author: User
Tags soap wsdl

Java WebService is implemented in a number of ways, typically in two ways:

A: SOAP-based Jax-WS (Java API for xml-based Web Services) such as: RI,AXIS,CXF

B: Rest-based jax-rs such as JERSEY,RESTEASY,CXF

You can see that CXF is not only used to develop SOAP-based WS, but also to develop a REST-based WS, so let's summarize the specifics of CXF.

SOAP style notation

1, defining the interface

Import Javax.jws.WebService; @WebService  Public Interface Hellofacade {    string SayHello (String msg);}

2, server interface and release

A, implement the interface:

Import Javax.jws.WebService; Import org.springframework.stereotype.Component; Import Com.bresume.core.ws.HelloFacade, @WebService @component  Public class Implements hellofacade{    @Override    public  string SayHello (String msg) {        System.out.println ("msg=" +msg);         return msg+ "_success";    }}
Hellofacadeimpl

b, Web. XML configuration:

    <!--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 configuration:

It is best to place the WS configuration file separately, introducing

<resource= "Spring-cxf.xml"/>

The establishment of Spring-cxf.xml jaxws:server or jaxws:endpoint Two configuration methods can be

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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/SPR Ing-beans-3.2.xsd Http://www.springframework.org/schema/context http://w                           Ww.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:endpointID= "Hellofacade"implementor= "#helloFacadeImpl"Address= "/soap/hello"/></Beans>

Server configuration completed, access to the WSDL, you can see the following, indicating that the server configuration is successful

3, Client Access

Importorg.apache.cxf.endpoint.Client;ImportOrg.apache.cxf.jaxws.JaxWsProxyFactoryBean;Importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;ImportCom.bresume.core.ws.HelloFacade; Public classTest {Static FinalString url = "Http://localhost:8081/ws/soap/hello";  Public Static voidMain (string[] args) {testjaxwsclient (); //testdynamicclient ();    }     Public Static voidtestjaxwsclient () {Jaxwsproxyfactorybean factory=NewJaxwsproxyfactorybean ();        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 voidtestdynamicclient () {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

-----------not finished------

WebService Summary of the CXF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.