This is the third day of my participation in Gwen Challenge

Accumulate over a long period, constant dripping wears away a stone 😄

Some third-party interfaces are of the WebService type. Therefore, you need to integrate WebService. SpringBoot provides the starter component of WebService. So integrating webServices is fairly simple

Join the rely on

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
        <version>We do</version>
</dependency>
<dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
</dependency>
Copy the code

The service side

Create a WebService interface

package com.gongj.webservice_server.service;

import com.gongj.webservice_server.DTO.UserDTO;
import javax.jws.WebService;

public interface IUserServer {

    UserDTO getUser(Long str);
}
Copy the code

Creating an entity Class

@Data
public class UserDTO {

    private Long id;
    private String name;
    private Integer age;
    private String address;
}
Copy the code

Create an implementation class for the WebService interface

package com.gongj.webservice_server.service.impl;

import com.gongj.webservice_server.DTO.UserDTO;
import com.gongj.webservice_server.service.IUserServer;
import org.springframework.stereotype.Service;

import javax.jws.WebService;

@Service
@WebService
public class UserServerImpl implements IUserServer {
    @Override
    public UserDTO getUser(Long id) {
        UserDTO user = new UserDTO();
        user.setId(id);
        user.setAddress("Pudong New Area, Shanghai");
        user.setAge(25);
        user.setName("gongj");
        returnuser; }}Copy the code

There is an annotation @webService, which I will only use on the implementation class. Here’s an introduction. Let’s look at its definition:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface WebService {
    String name(a) default "";

    String targetNamespace(a) default "";

    String serviceName(a) default "";

    String portName(a) default "";

    String wsdlLocation(a) default "";

    String endpointInterface(a) default "";
}
Copy the code
  • Name: correspondingwsdl:portTypeThe default value is the name of the Java class or interface
  • TargetNamespace: namespace. It is written as the reverse order of the interface package name. The default value is also the reverse order of the interface package name. The correspondingwsdl:definitions:targetNamespaceThe label,
  • ServiceName:WebServiceIs the service name corresponding towsdl:service, the default value isWebServiceName of interface implementation class + “Service”, example:UserServerImplService
  • Corresponding portName:wsdl:portThe default value is:WebServiceName of interface implementation class + “Port”, example:UserServerImplPort
  • WsdlLocation: Specifies for definitionWebServiceThe address of the WSDL document
  • EndpointInterface:WebServiceInterface full path

Create the WebService configuration class

package com.gongj.webservice_server.config;

import com.gongj.webservice_server.service.IUserServer;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    @Autowired
    private IUserServer userServer;
    Note that beanName cannot be dispatcherServlet *@return* /
    @Bean
    public ServletRegistrationBean cxfServlet(a) {
        return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
    }

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


    @Bean
    public Endpoint endpoint(a) {
        EndpointImpl endpoint = new EndpointImpl(springBus(), userServer);
        endpoint.publish("/api");
        returnendpoint; }}Copy the code

Start the service, visit: http://localhost:8080/webservice

Click the link to jump to a page that shows the WSDL service description document. This document also needs a brief introduction, maybe once you connect to a third-party system and give you a WSDL document to look at for yourself,Note: WSDL documents are viewed from the bottom up.

A WSDL document


      
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns:tns="http://impl.service.webservice_server.gongj.com/" 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="UserServerImplService" 
targetNamespace="http://impl.service.webservice_server.gongj.com/">


<! -- Data type used by the Web Service -->
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://impl.service.webservice_server.gongj.com/" elementFormDefault="unqualified" targetNamespace="http://impl.service.webservice_server.gongj.com/" version="1.0">

  <xs:element name="getUser" type="tns:getUser"/>

  <xs:element name="getUserResponse" type="tns:getUserResponse"/>

<! -- request parameter type of getUser method -->
  <xs:complexType name="getUser">
    <xs:sequence>
      <xs:element minOccurs="0" name="arg0" type="xs:long"/>
    </xs:sequence>
  </xs:complexType>

<! -- Response parameter type of getUser method -->
  <xs:complexType name="getUserResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="tns:userDTO"/>
    </xs:sequence>
  </xs:complexType>

<! The specific type of the response parameter to the getUser method
  <xs:complexType name="userDTO">
    <xs:sequence>
      <xs:element minOccurs="0" name="address" type="xs:string"/>
      <xs:element minOccurs="0" name="age" type="xs:int"/>
      <xs:element minOccurs="0" name="id" type="xs:long"/>
      <xs:element minOccurs="0" name="name" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
  </wsdl:types>
  
  <! -- message: used to define the SOAP message structure
  <wsdl:message name="getUser">
    <wsdl:part element="tns:getUser" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getUserResponse">
    <wsdl:part element="tns:getUserResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  
  <! Operation: the method defined in the interface input: the input of method getUser output: the output of method getUser Input output refers to the definition of message above -->
  <wsdl:portType name="UserServerImpl">
    <wsdl:operation name="getUser">
      <wsdl:input message="tns:getUser" name="getUser">
    </wsdl:input>
      <wsdl:output message="tns:getUserResponse" name="getUserResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  
  <! Soap :binding style="document">: a document (XML) <input><output> method <soap:body Use ="literal" />
  <wsdl:binding name="UserServerImplServiceSoapBinding" type="tns:UserServerImpl">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getUser">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="getUser">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getUserResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  
  <! -- name: specifies the client's container class/factory class. Binding: references the binding tag above. Port name: specifies the container class.
  <wsdl:service name="UserServerImplService">
    <wsdl:port binding="tns:UserServerImplServiceSoapBinding" name="UserServerImplPort">
      <soap:address location="http://localhost:8080/webservice/api"/>
    </wsdl:port>
  </wsdl:service>
  
</wsdl:definitions>
Copy the code

The client

Join the rely on

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>We do</version>
</dependency>
Copy the code

call

public static void main(String[] args) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/webservice/api? wsdl");
        ObjectMapper mapper = new ObjectMapper();
        try {
            // invoke(" method name ", parameter 1, parameter 2, parameter 3....) ;
            Object[] objects = client.invoke("getUser".99L);
            System.out.println(mapper.writeValueAsString(objects[0]));
        } catch(java.lang.Exception e) { e.printStackTrace(); }} Output: {"address":"Pudong New Area, Shanghai"."age":25."id":99."name":"gongj"}
Copy the code
  • If you have any questions or errors in this article, please feel free to comment. If you find this article helpful, please like it and follow it.