SoFunction
Updated on 2025-03-02

Detailed explanation of springboot's example of publishing and calling interface using webservice

springboot uses webservice to publish and call interfaces

Add the following dependencies:

<!-- cxfFramework dependency  -->
	<dependency>
		<groupId></groupId>
		<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
		<version>3.2.4</version>
	</dependency>

Server service code:

import ;
import ;
import ;
/**
  * TODO (describes the function of this class)
  * @author zyl
  * @date March 27, 2019
  */
/**
  * Create a service interface
  * @author oKong
  *
  */
@WebService()
public interface HelloWebService {
    @WebMethod
    public String Hello(@WebParam(name="name") String name);
}

Server-side implementation class code:

import ;
import ;
import ;
import ;
/**
  * TODO (describes the function of this class)
  * @author zyl
  * @date March 27, 2019
  */
@WebService(
        targetNamespace = "", //wsdl namespace        serviceName = "HelloWebService",                 //portType name When the client generates the code, it is the interface name        endpointInterface = "")//Specify the interface class to publish webservcie, this class also needs to access the @WebService annotation@Configuration
public class HelloWebServiceImpl implements HelloWebService{
    @Override
    public String Hello(@WebParam(name="name") String name) {
        ("Welcome"+name);
        return "Welcome"+name;
    }
}

Server-side publishing service category:
My port is set to 9999, so my service address is http://127.0.0.1:9090/ws/helloWebService?wsdl

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
  * cxf configuration class
  * @author oKong
  *
  */
@Configuration
public class CxfWebServiceConfig {
    @Autowired
    private Bus bus;
    @Autowired
    private HelloWebService helloWebService;
    @Bean("cxfServletRegistration")
    public ServletRegistrationBean dispatcherServlet() {
        //Register servlet intercept requests starting with /ws Not set. The default is: /services/*         return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
     }
     /*
      * Post an endpoint
      */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, helloWebService);
        ("/helloWebService");//Release address        return endpoint;
    }
}

Client call service code:
Choose one of the following two methods

import ;
import ;
import ;
/**
  * @ClassName:CxfClient
  * @Description:webservice client:
  * This class provides two different ways to call the webservice service
  * 1: Agent factory method
  * 2: Dynamically call the webservice
  * @author Jerry
  * @date: April 10, 2018 at 4:14:07 pm
  */
public class CxfClient {
    public static void main(String[] args) {
        CxfClient.main1();
//        CxfClient.main2();
    }
    /**
      * 1. The agent factory method requires getting the other party's interface address
      */
    public static void main1() {
        try {
            // Interface address            String address = "http://127.0.0.1:9090/ws/helloWebService?wsdl";
            // Agent factory            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // Set the proxy address            (address);
            // Set the interface type            ();
            // Create a proxy interface implementation            HelloWebService us = (HelloWebService) ();
            // Data preparation            String userId = "zz";
            // Call the method call of the proxy interface and return the result            String result = (userId);
            ("Return result:" + result);
        } catch (Exception e) {
            ();
        }
    }
    /**
      * 2: Dynamic call
      */
    public static void main2() {
        // Create dynamic client        JaxWsDynamicClientFactory dcf = ();
        Client client = ("http://127.0.0.1:9090/ws/helloWebService?wsdl");
        // If you need a password, you need to add a username and password        // ().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("Method Name", parameter 1, parameter 2, parameter 3...).;            objects = ("getUserName", "maple");
            ("Return data:" + objects[0]);
        } catch ( e) {
            ();
        }
    }
}

This is the article about springboot's webservice publishing and calling interfaces. For more related contents of springboot's webservice publishing and calling interfaces, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!