springboot2.x Integrated Web Service

Posted by vanderlay on Tue, 18 Feb 2020 05:23:40 +0100


Recently, when some parts of the project call each other, they are not realized by Http(post/get), but by web service. Although they have been exposed to the way of web service before, they haven't had a chance to study it. They just imitate and write according to the rules and don't build a web service by themselves. I'm free these days. I'd like to take notes. I hope you can leave a message if you don't write well or express clearly.

What is web service

WebService is a kind of remote calling technology which is cross programming language and cross operating system platform.

I also copied this question from Baidu, but I still hope you can have a look at it and understand some of the terms.

  1. Remote call technology: remote call refers to that program A on one device can call method B on another device (my understanding is that you can see my method name, parameter type and number of parameters, rather than the definition of A url interface). -)
  2. XML: (Extensible Markup Language) extensible markup language. Facing short-term temporary data processing and world wide network is the foundation of Soap.
  3. Soap: (Simple Object Access Protocol) simple object access protocol. Is the communication protocol for the XML Web Service. When the user finds your WSDL description document through UDDI, he can call one or more operations in the web service you established through soap. Soap is the specification of calling methods in the form of XML documents. It can support different underlying interfaces, such as HTTP(S) or SMTP.
  4. WSDL: (Web service description language) a WSDL file is an XML document that describes a set of SOAP messages and how to exchange them. In most cases, it is automatically generated and used by software.

Why use web service? Isn't http enough for you?

First of all, make it clear: Web service is also a transport mode of http. Why do you say that?
First of all, web service is based on soap Protocol, which is a special version of HTTP POST. It follows a special xml message format: content type is set as: text/xml any data can be xml.
HTTP is the most common transport tool for SOAP messages

Advantages and disadvantages:

  1. The methods and required parameters implemented in the interface are clear at a glance
  2. Don't worry about the case problem (just give the class to you, if you write it wrong, the code won't run)
  3. Don't worry about Chinese urlencode
  4. It is not necessary to declare the authentication (account, password) parameter multiple times in the code
  5. Passing parameters can be array, object, etc

How to integrate Web service in springboot

Server side

1. Preliminary preparation

	 Development tool: idea
	 springboot version: 2.2.4

2. pom.xml

maven introduces two dependencies:


Explain these two jar packages:

  • spring-boot-starter-web-services:
    There's nothing to say about this. If you don't add spring boot to integrate Web service into this package, what kind of quartz package will you add?
  • cxf-spring-boot-starter-jaxws
    This package is for web service publishing.
    We know that Web Service is a technical specification that enables applications to communicate with different programming languages on different platforms. The implementation of this technical specification is based on XML form protocol (SOAP) or RESTFUL form.
    If we use these two methods to communicate, we must have standardized work, that is, jaxws specification and jar RS specification.
    cxf is exactly the concrete implementation of these two specifications.
    In other words: JAX-WS is the standard, CXF and Axis are the specific framework implementation.
    Attach a picture:

3. interface class

Create a new interface WebServiceDemoService

package com.cj.webservice.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

public interface WebServiceDemoService {
    public String hello(@WebParam(name = "name") String name);

@webservice: indicates that it is a webservice service. This class will be brought when publishing.
@WeBMethod: method published in webservice
@WebParam: the alias of the parameter is neither written nor affected, but the parameter appears to be arg0 in the wsdl, which is not easy to understand.

4. implementation class

package com.cj.webservice.service;
import org.springframework.stereotype.Service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
 * Description:
 * @author caojing
 * @create 2020-02-14-14:56
@WebService(serviceName = "fileDeviceService",//External service name
        targetNamespace = "http://service.webservice.cj.com",//Specify the namespace you want, usually using package name inversion
        endpointInterface = "com.cj.webservice.service.WebServiceDemoService")
public class WebServiceDemoServiceImpl implements WebServiceDemoService {
    public String hello(String name) {
        return "hello" + name;

I have written the notes that should be written, brothers. Take a look and have a look.

5. configuration class

package com.cj.webservice.config;

import com.cj.webservice.service.WebServiceDemoService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.cxf.transport.servlet.CXFServlet;

import javax.xml.ws.Endpoint;

 * Description: webservice configuration class
 *  * @author caojing
 * @create 2020-02-14-14:47
public class WebServiceConfig {
    private WebServiceDemoService webServiceDemoService;

    //*After this method is annotated: the wsdl access address is
    //     *After removing the comment: the wsdl access address is:
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");

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

    @Bean(name = "webServiceDemoEndPoint")
    public Endpoint webServiceDemoEndPoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), webServiceDemoService);
        return endpoint;
    //If there are other services, continue to add

Both methods of cxfServlet and springBus should not be changed. If a new service comes in, add another endPoint method to the service.

6.0 start up project

The log as shown above indicates a successful start.
Access address:
As shown in the figure below:

At this point, the code of the service section has been completed. We create a web service. This server is the parameter you pass and returns to you the "heelo" + parameter.

How to test Web Service

Here I say two ways to test the web service:

  • Use tools to test by yourself, don't care how to call the client (the method is optional)
    Recommend an easy to use web service testing tool
    Open tool:

    Click ok, as shown:
  • Internal use
    Because we need to call this service internally, our way is to pack jar packages and put them on maven warehouse (our private maven Library). The projects that need to be used can be pulled down directly by maven.
    idea packaging method:
    File -> Project Structure

    name: com CJ WebService service
    Then add the package to the class file to be packed

    All the way to com.cj.webservice.service

    Click "+" to add Directory Content,

    It should be noted that class files are added, that is, compiled files.

    Finally, as shown in the figure:

    End of apply, then
    Build - > build artifacts to generate jar package. The location of jar package has been specified during creation. You can go back and have a look at the forgotten children's shoes.

Client use:

The first step is to import the jar package just made into idea as lib. This will not happen. Please go out and turn right and Baidu yourself.

1. pom file:


2. Write a test class:

import com.cj.webservice.service.WebServiceDemoService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 * Description:
 * @author caojing
 * @create 2020-02-14-15:37
public class test {
    static WebServiceDemoService webServiceDemoService;
    public static void main(String[] args) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        webServiceDemoService = (WebServiceDemoService) factory.create();


After so long, it's finally over......
Let me summarize a few points of integrating web service in spring boot.

  1. First, add the jar dependency used.
  2. Write your own external service class with @ Webservice @ webmethod annotation. Note: pay attention to several properties in @ Webservice. serviceName, targetNamespace and endpointInterface are three external attributes of your service.
  3. Write the config class, that is, publish. The springBus and cxfServlet do not need to be changed. The only thing that needs to be changed is Endpoint. If there is a new web service service, add a method whose return value is Endpoint directly. The content of the method should follow the example.


Server code
Client code

31 original articles published, 92 praised, 140000 visitors+
Private letter follow

Topics: xml Apache Spring Maven