Take you around Web Services [2] CXF in minutes

Keywords: Java Spring Apache git xml

In practice, we have been using JAX-WS to build Web Service service, which is very stable and efficient.

But I'm still curious about other open source frameworks such as CXF/Axis2/Spring WS.

Because of the trust and love for Apache, the CXF Web Service will certainly not disappoint.

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

For WebService and CXF profiles, I won't go into details here, but for those who don't understand it, please take a step first. Take you to Web Services [1] JAX-WS in minutes

This article tries to publish CXF Web Service and Spring Hosted CXF Web Service from Servlet, which can take you to play 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

For those who need to practice in this field, please collect this blog. By that time, you only need to run Demo locally, and everything will be clear.

1. Servlet publishes CXF WebService

a.mavn depends on 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>

Dependent Jar s are basically sum/apache/codehaus support, which has been washed out over the years and is stable and efficient.

b. Service implementation:

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface UserService {

    /**
     * Web Service method for executing tests (with reference)
     */
    @WebMethod
    String sayHi(@WebParam(name = "name") String name);
}
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class UserServiceImpl implements UserService {

    @WebMethod
    public String sayHi(String name) {
        return "Hi, " + name + "! ";
    }
}

c.Servlet implementation:

public class WebServicesServlet extends CXFNonSpringServlet {
    private static final long serialVersionUID = -5314312869027558456L;

    @Override
    protected void loadBus(ServletConfig servletConfig) {
        super.loadBus(servletConfig);
        Endpoint.publish("/UserService", new UserServiceImpl());
    }
}

You haven't mistaken CXF's non-integrated Spring Servlet, which is called CXF NonSpring Servlet, for it's a bit vulgar.

It's also very simple to use, just implement the loadBus method in the org.apache.cxf.transport.servlet.CXFNonSpring Servlet.

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>

Start the project, visit: http://localhost:4042/cxf-demo/ws/UserService?

2. Spring Hosting CXF WebService

a.mavn depends on 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 just need to rely on Spring Web, where Spring Context hosts the implementation classes of Web Service services.

Service implementation is consistent with web.xml and Servlet publishing, and no special processing is required, so it is not posted here.

b. Hosting Web Service Services to Spring Context

<beans xmlns="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">

    <bean id="userService" class="com.rambo.cxf.spring.demo.impl.UserServiceImpl"/>

    <jaxws:endpoint id="userServiceWs" implementor="#userService" address="/UserService"/>
</beans>

3. Summary

Servlet publishing CXF Web Service relies on fewer open source libraries, which means that the probability of problems is small.

Each time a new Web Service service is added, the Web Services Servlet class needs to be modified.

Spring Context hosts Web Service implementation classes, and new service classes can be configured in cxf-servlet.xml.

With excellent containers to manage for you, you'll be comfortable. Large and complex Web Services recommend configuring Spring for use.



Posted by MichaelR on Fri, 05 Jul 2019 11:44:05 -0700