This is the 18th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

WebService learning

WebService profile

WebService interface, CXF framework, with XML configuration exposed interface, the interface provider can use the object has implemented the serialized interface to receive, of course, the general interface data structure is not so simple, then we can also be corresponding to the object we want to receive data add List or object attributes to achieve. The CXF framework automatically converts THE XML data into the desired object, and the underlying transfer is actually the TRANSFER of XML data over HTTP (you can write an interceptor to get the sent XML file)

In plain English, it is the process of generating, delivering, and parsing XML documents. The client generates an XML file and sends it to the server over the network. The server parses the XML, obtains the return value of the parameter execution method, generates an XML file and sends it to the client. The client parses the XML and displays the data.

XML data transmission is generally Webserver ce, partial bank some more; Json is a little bit more Internet oriented. To expose the interface, we can write a controller and use @requestMapping (” XXXX “) to specify the interface call address; The underlying transmission is json String, we can use String to receive, can also use the object to receive (serialization interface), the general framework has Ali fastJSON with springMvc built-in Jackson

Expose the WebService service

Scenario 1: Spring Boot2.x

Environment: Spring Boot2.x

  1. Add CXF dependencies
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> The < version > 3.2.5 < / version > < / dependency >Copy the code
  1. Add @WebService annotations to the external Facade interface and @WebMethod annotations to the methods
@WebService
public interface AuthServiceFacade {
    @WebMethod
    AuthResponse auth(AuthRequest request);
}
Copy the code
  1. Add an annotation @webService annotation to the implementation class
@Component
@Service
@WebService(targetNamespace = "http://webservice.company.com/",endpointInterface = "com.***.auth.service.facade.AuthServiceFacade")
public class BankCardSingleAuthServiceImpl extends AbstractAuthService
        implements AuthServiceFacade {}
Copy the code
  1. Set the WebServiceConfig

Scenario 2: Exposure through CommandLineRunner

//SpringBoot will automatically load and expose the service after startup. The service IP can be dynamically adapted
@Configuration
@Order(1)// The number indicates the initialization sequence
public class CxfConfig implements CommandLineRunner {

    @Value("${url:localhost}")
    private String url ;

    @Override
    public void run(String... args){
        Service service = new Service();
        Endpoint.publish("http://"+url+":8080/service", service); }}Copy the code

Wsdl file

Important attributes

Description of important labels

· Types – Containers defined by a schema that defines tag structures for reference by message

· Message – Abstract typed definition of the data structure of the communication message. References tags defined in types

· Operation – An abstract description of the operations supported in the service. An operation describes a pair of request messages and response messages for an access point.

· portType – An abstract set of operations supported by an access entry point type that can be supported by one or more service access points.

· Binding – Binding of a specific protocol and data format specification for a specific port type.

· Service – A collection of related service access points

· Port – Defined as a single service access point bound to a combination of protocol/data format and a specific Web access address.

Common mistakes

The package WSDL file cannot be generated as a Maven package

Targe needs to be removed from maven package and then packaged to generate

@Autowrite cannot be injected into the @WebService annotation class

This problem gave me a headache and I didn’t expect to do it by using static blocks.

@WebService(endpointInterface = "***", serviceName = "***")
@Component
public class WebService implements IWebService {
    @Autowired
    private static ISecondService secondService;
// Static is used here, so service belongs to class
    static {
        // Load a single configuration file to instantiate the ApplicationContext container
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring\\dubbo.xml");
        secondService = (ISecondService) ctx.getBean("secondService");
    }
Copy the code

SoupUI can debug but does not receive requests when registering with other services. This problem may be due to different formats between the two services. We can use an adapter design pattern to deal with this problem.