Take you to the Web Services "2" CXF in minutes

Source: Internet
Author: User
Tags hosting

In practice, the WebService service has been built using Jax-WS, and the service is very stable and efficient.

But still more curious other WebService open source framework, such as: Cxf/axis2/spring ws and so on.

Based on the trust and love of Apache, CXF WebService certainly won't disappoint.

So it took a little time to introduce CXF into the project practice, one more choice.

For WebService and CXF introduction here do not repeat, do not understand the classmate please move: minutes to take you to play the Web Services "1" Jax-ws

This trial released CXF WebService and Spring hosted CXF WebService Two ways from the Servlet to take you to the CXF.

Servlet publishes CXF WebService git demo address:Http://git.oschina.net/LanboEx/cxf-demo

Spring hosted CXF WebService git demo address: Http://git.oschina.net/LanboEx/cxf-spring-demo

Need to have this practice of students, please bookmark this blog, by the time only the Demo in the local run up, everything is clear.

1. Servlet release CXF WebService

A.mavn Dependent Jar:

       <!--Web Container Support -        <Dependency>            <groupId>Javax.servlet</groupId>            <Artifactid>Javax.servlet-api</Artifactid>            <version>3.1.0</version>        </Dependency>        <!--Apache CXF WebService -        <Dependency>            <groupId>Org.apache.cxf</groupId>            <Artifactid>Cxf-rt-frontend-jaxws</Artifactid>            <version>3.1.11</version>        </Dependency>        <Dependency>            <groupId>Org.apache.cxf</groupId>            <Artifactid>Cxf-rt-transports-http</Artifactid>            <version>3.1.11</version>        </Dependency>

The jars that rely on are basically sum/apache/codehaus supported, and these jars have been washed for years and are stable and efficient.

B. Service implementation:

= SOAPBinding.Style.RPC)publicinterface  userservice {    /**      * WebService method for executing tests (with reference)      */     @WebMethod    = "name") String name);}
@WebService= SOAPBinding.Style.RPC)publicclassimplements  UserService {    @WebMethod    public  string Sayhi (string name) {        return "Hi," + name + "!" ;    }}

C.servlet implementation:

  Class  Webservicesservlet extends   Cxfnonspringservlet { private  static  final  long  serialversionuid =-    5314312869027558456l protected  void   Loadbus (ServletConfig servletconfig) { super  .loadbus (ServletConfig); Endpoint.publish ( "/userservice", new  

You're not mistaken. CXF provides a Servlet that does not integrate Spring is called Cxfnonspringservlet, is not a bit vulgar.

It is also very simple to use, to implement the Loadbus method in Org.apache.cxf.transport.servlet.CXFNonSpringServlet.

D.web.xml configuration:

    <servlet>        <Servlet-name>Cxfwsservlet</Servlet-name>        <Servlet-class>Com.rambo.cxf.demo.ws.servlet.WebServicesServlet</Servlet-class>    </servlet>    <servlet-mapping>        <Servlet-name>Cxfwsservlet</Servlet-name>        <Url-pattern>/ws/*</Url-pattern>    </servlet-mapping>

launch Project, visit: http://localhost:4042/cxf-demo/ws/UserService?wsdl

2. Spring hosted CXF WebService

A.mavn Dependent Jar:

<!--Spring -        <Dependency>            <groupId>Org.springframework</groupId>            <Artifactid>Spring-web</Artifactid>            <version>4.3.7.RELEASE</version>        </Dependency>        <!--Apache CXF WebService -        <Dependency>            <groupId>Org.apache.cxf</groupId>            <Artifactid>Cxf-rt-frontend-jaxws</Artifactid>            <version>3.1.11</version>        </Dependency>        <Dependency>            <groupId>Org.apache.cxf</groupId>            <Artifactid>Cxf-rt-transports-http</Artifactid>            <version>3.1.11</version>        </Dependency>

You only need to rely on Springweb, where Spring Context is hosting the implementation class for the WebService service.

The service implementation and Web. XML are consistent with the Servlet release and do not require special processing, and are not posted here.

B. Hosting the WebService service to Spring Context

<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jaxws= "Http://cxf.apache.org/jaxws"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">    <BeanID= "UserService"class= "Com.rambo.cxf.spring.demo.impl.UserServiceImpl"/>    <Jaxws:endpointID= "Userservicews"implementor= "#userService"Address= "/userservice"/></Beans>
3. Summary

The Servlet publishes CXF WebService relies on less open source libraries, which means that the probability of a problem is small;

Each time you add a WebService service, you need to modify the Webservicesservlet class.

Spring Context Managed WebService implementation class, the new service class can be configured in the Cxf-servlet.xml;

There are excellent containers for you to manage, you will be very comfortable, large complex WebService recommended to configure Spring use.



Take you to the Web Services "2" CXF in minutes

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.