SpringBoot uses CXF to integrate Web Services

Keywords: Java Apache SpringBoot Spring xml

1. Write in front

Web Service is familiar and unfamiliar to me. I haven't seen him for nearly six or seven years. I don't have much specific introduction. What I want to know about Baidu Encyclopedia is very detailed.

The reason why we suddenly studied Web Service is that we received a requirement to make an adaptation layer for XX project. Their original system was made with web service, so...
Let's see how this ancient technology can be combined with SpringBoot, the most popular framework today.

2. SpringBook Integrated Web Service

2.1 Import Dependency

compile('org.springframework.boot:spring-boot-starter-web-services',
            'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.2.5',
            'org.apache.cxf:cxf-rt-frontend-jaxws:3.1.6',
            'org.apache.cxf:cxf-rt-transports-http:3.1.6')

I build the project with Gradle, and like Maven, add the jar dependencies above.

2.2 Developing Web Service Interface

/**
 * serviceName Service Name
 * targetNamesPace : Usually the interface packages are inverted, you can also customize
@javax.jws.WebService(serviceName = "gzcxfw_wx_webserviceService",
        targetNamespace = "http://36.101.208.59:8090/axis/services/bdcgzcxfw_wx")
public interface WebService {
    /**
     *
     * @param wxid     Wechat ID
     * @param xm       Full name
     * @param sfzh     ID number
     * @param sjh      Cell-phone number
     * @param macId    Reserved users
     * @param password Password
     * @return Query result Base64 string
     */
    @WebMethod(operationName = "gzcxfw_hlw_wxrz_Info_cs")
    @WebResult(name = "gzcxfw_hlw_wxrz_Info_csReturn")
    String gzcxfwHlwWxrzInfoCs(@WebParam(name = "WXID", targetNamespace = "http://36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String wxid, @WebParam(name = "XM") String xm,
                               @WebParam(name = "SFZH", targetNamespace = "http://36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String sfzh, @WebParam(name = "SJH") String sjh,
                               @WebParam(name = "mac_id", targetNamespace = "http://36.101.208.59:8090/axis/services/bdcgzcxfw_wx") String macId, @WebParam(name = "password") String password
    );

2.3 Implementation Class

/**
 * @author yueli
 * @date 2019-08-05 19:17
 */
@javax.jws.WebService(serviceName = "gzcxfw_wx_webserviceService",
        targetNamespace = "http://36.101.208.59:8090/axis/services/bdcgzcxfw_wx")
public class WebServiceImpl implements WebService {

    private static Logger logger = LoggerFactory.getLogger(CatalogInfoImpl.class);


    @Override
    public String gzcxfwHlwWxrzInfoCs(String wxid, String xm, String sfzh, String sjh, String macId, String password) {
        logger.info("gzcxfwHlwWxrzInfoCs: Participation- wxid:{}, xm:{}, sfzh:{}, sjh:{}, macId:{}, pawd:{}", wxid, xm, sfzh,
                macId, password);

        return wxid + ":" + sfzh;
    }

Implementing classes is simple. It's no different from what we usually write.

2.4 Release

package com.tusdao.base.configuration;

import com.tusdao.TusdaoWebserviceApplication;
import com.tusdao.webservice.service.WebService;
import com.tusdao.webservice.service.impl.WebServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;


/**
 * @author yueli
 * @date 2019-08-05 19:24
 */
@Configuration
@ComponentScan(basePackageClasses = TusdaoWebserviceApplication.class)
public class CxfConfig {
    @SuppressWarnings("all")
    @Bean(name = "cxfServletRegistration")
    public ServletRegistrationBean dispatcherServlet() {
        //Create a service and specify a service name
        return new ServletRegistrationBean(new CXFServlet(), "/axis/services/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }


    @Bean
    public WebService webService() {

        return new WebServiceImpl();
    }

    /**
     * Register Web Service DemoService Interface to Web Service Service Service Service
     *
     * @return
     */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), webService());
        endpoint.publish("/bdcgzcxfw_wx");
        endpoint.getInInterceptors().add(new ServerNameSpaceInterceptor());
        //endpoint.getInInterceptors().add(new InInterceptor());
        return endpoint;
    }

}

This is simple. We can copy the past directly when we use it.
Finally, start the project.
After startup, we enter the project address directly: http://localhost 8090/Designated Service Name

You will see the generated ssdl. This is the end of the basic building. The test is not written here, you can use the CSS to generate the client, or directly use http to send xml format data requests.

3. Summary

springboot integrated Web service development using CXF is very simple and does not need to be deployed to external tomcat separately, which brings us a good experience for those who are familiar with springboot development.

If you want a complete example, please look at it. https://github.com/yuelicn/sp...
Or clone directly > > > https://github.com/yuelicn/sp...

Posted by rahnel on Wed, 14 Aug 2019 06:30:35 -0700