SpringBoot integrates WebServices (Apache CXF JAX-WS) (1) Getting started example

table of Contents

1. Introduction

Introduction to Web Services

soap protocol

Apache CXF

Second, the WebService framework

Apache CXF

Apache axis2

Spring Web Services

Three, Apache CXF JAX-WS example

1. Create a SpringBoot project and add jaxws dependency

2. Create an interface

3. WebServiceConfig configuration class

4. Start the project

5. Test WebService

(1) Use idea to generate client

(2) Test


1. Introduction

Introduction to Web Services

Web Services are a type of RPC. Web Services uses SOAP as the transport protocol, WSDL as the service description language, and UDDI as the service registration discovery (although it has not been developed). Although the related agreements of Web Services have not been updated since 2007, in some financial institutions such as banks, Web Services are still being used extensively. There are many frameworks for WebService, such as Axis2, XFire, CXF and so on. Apache Cxf is one of the best and most vital, and the CXF framework is not only a Web Services framework, but even RESTful-style services can be realized through CXF.

soap protocol

Simple Object Access Protocol (SOAP) is an XML-based protocol that can be used in conjunction with many existing Internet protocols and formats, including Hypertext Transfer Protocol (HTTP), Simple Mail Transfer Protocol (SMTP), and more Use Internet Mail Extension Protocol (MIME), based on the "universal" transport protocol, is an advantage of SOAP.

Apache CXF

JAX-WS stands for JavaTM API for XML-Based Web Services 

AX-RS stands for JavaTM API for RESTful Web Services

Second, the WebService framework

The more popular WebService frameworks are as follows:

Apache CXF

http://cxf.apache.org/docs/springboot.html

Apache axis2

http://axis.apache.org/axis2/java/core/

Spring Web Services

https://docs.spring.io/spring-ws/docs/3.0.10.RELEASE/reference/#resources

Three, Apache CXF JAX-WS example

1. Create a SpringBoot project and add jaxws dependency

SpringBoot version 2.2.4.RELEASE

      <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.3.9</version>
        </dependency>

If the release fails, pay attention to the version gap not too big

https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws/3.3.9

2. Create an interface

ServiceDemo
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(name = "ServiceDemo",
        targetNamespace = "http://www.baidu.com")
public interface ServiceDemo {

    @WebMethod
    String emrService(@WebParam String data);

}

 ServiceDemoImpl

import org.springframework.stereotype.Component;
import javax.jws.WebParam;
import javax.jws.WebService;

@Component
@WebService(name = "ServiceDemo",
        targetNamespace = "http://www.baidu.com",
        endpointInterface = "com.asyf.demo.service.ServiceDemo")
public class ServiceDemoImpl implements ServiceDemo {

    @Override
    public String emrService(@WebParam String data) {
        if (null == data || "".equals(data.trim())) {
            return "传入的参数为空";
        }
        return "调用成功,传入的参数是:" + data;
    }

}

3. WebServiceConfig configuration class

import com.asyf.demo.service.ServiceDemo;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfig {

    @Autowired
    private ServiceDemo serviceDemo;

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

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), serviceDemo);
        endpoint.publish("/ws/api");
        return endpoint;
    }

}

4. Start the project

The service is published successfully. Visit http://localhost:8080/services/ws/api?wsdl and you can see the page as shown in the figure.

5. Test WebService

(1) Use idea to generate client

Create a new client package to store the generated files, right click to see WebServices

Click ok to generate the client file

Client file is successfully generated

(2) Test

Use the main function to test

  public static void main(String[] args) {
        String address = "http://127.0.0.1:8080/services/ws/api?wsdl";
        // 代理工厂
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        // 设置代理地址
        jaxWsProxyFactoryBean.setAddress(address);       
        // 设置接口类型
        jaxWsProxyFactoryBean.setServiceClass(ServiceDemo.class);
        // 创建一个代理接口实现
        ServiceDemo cs = (ServiceDemo) jaxWsProxyFactoryBean.create();
        // 数据准备
        String data = String.valueOf(new Date());
        // 调用代理接口的方法调用并返回结果
        String rs = cs.emrService(data);
        System.out.println("==============\n返回结果:" + rs);
    }

 Console printing

 

Guess you like

Origin blog.csdn.net/cs373616511/article/details/112754986