Soap from entry to actual combat
Reference article: https: //howtodoinjava.com/spring-boot/spring-soap-client-webservicetemplate/
Technology used: springboot + jaxb
What is a .soap
Online is explained:
SOAP is a simple XML-based protocol that enables applications to exchange information over HTTP. Or more simply: SOAP is a protocol for accessing a Web Service.
My understanding is:
- 1. He is a network transmission protocol
- 2. His foundation is http
- 3. He is no longer commonly used to transfer content text but xml
The difference between two .soap and http
- 1.soap transfer content is xml, http transmission of the content is text
- 2.soap the wsdl file can expose interfaces to access the object, the caller can see the detailed structure of access to the object. http only transmit text does not describe the information, if required front-end back-end interface information needed to use third-party plug-ins or write their own.
Three .Wsdl
wsdl is short for Web Sercivice Descipe Language, that he is describing a Web service file. Description of routing services, method name, return value, and so on.
The basic structure of four .Wsdl
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
...
...
</soap:Header>
<soap:Body>
...
...
<soap:Fault>
...
...
</soap:Fault>
</soap:Body>
</soap:Envelope>
Envelope is mandatory to use, I think it is constrained way to reference it, to emphasize our content is defined as soap xml news
Five .soap real column
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.definesys.com/xml/employee"
targetNamespace="http://www.definesys.com/xml/employee" elementFormDefault="qualified">
<xs:element name="EmployeeDetailRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" type="tns:Employee"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="EmployeeDetailResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" type="tns:Employee"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Employee">
<xs:sequence>
<xs:element name="code" type="xs:string"></xs:element>
<xs:element name="name" type="xs:string"></xs:element>
<xs:element name="email" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
namespace: http: //www.definesys.com/xml/employee own take
targetNamespace = "http://www.definesys.com/xml/employee" own definition of
EmployeeDetailRequest: accessible entrance, which is required to access the content of parameters
EmployeeDetailResponse: return of exports, the contents of which is returned when the return parameter
VI. Use Springboot combat
1. Create a Web Service
File-------> New------->Project-------->Spring Initializer ------>web,web service
2. Import jaxb generated dependent objects such pojo
1. Add the following to the pom
```xml
```
$ {Project.basedir} / src / main / java: the root directory is defined in the output file
com.example.webservice.entity: generating a package name pojo
Pojo generated as follows:
Employee: real ordinary java object
EmployeeDetailRequest: Emploee encapsulates objects soap can be resolved into a normal request Employee object
EmployeeDetaiResponse: The Common Object Employee object packaged into Soap
package-info.java: namespace binding target output
3. explanation related annotations
@XmlAccessorType(XmlAccessType.FIELD)
The definition of the class level, the class fields are written in xml
@XmlType(name = "Employee", propOrder = { "code", "name", "email"})
Class definition level, the field definitions written in the order xml
@XmlElement(required = true)
Field defines the level of the field written to the xml
@XmlRootElement(name = "EmployeeDetailRequest")
Class definition level, name refers to the name when mapping to xml
4. Write a configuration file to generate wsdl
package com.example.webservice.config;
import org.springframework.beans.factory.parsing.DefaultsDefinition;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.WsdlDefinition;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;
@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean messageDispathcherServlet(ApplicationContext applicationContext){
MessageDispatcherServlet servlet=new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet,"/cms/*");
}
@Bean(name="employee")
public WsdlDefinition defaultWsdl11Defination(XsdSchema employeeSchema){
DefaultWsdl11Definition wsdl=new DefaultWsdl11Definition();
wsdl.setPortTypeName("EmployeePort");
wsdl.setLocationUri("/cms/employee-detail");
wsdl.setTargetNamespace("http://www.definesys.com/xml/employee");
wsdl.setSchema(employeeSchema);
return wsdl;
}
@Bean
public XsdSchema employeeSchema(){
return new SimpleXsdSchema(new ClassPathResource("employee.xsd"));
}
}
Root path specified in the method of our project access paths: messageDispathcherServlet
defaultWsdl11Defination: we write the writing of incomplete xsd files into one complete and return the wsdl wsdl file, add a PortTypeName (it seems useless, l like port number), LocationUri (path), TargetNamespace (no Dayong )
employeeSchema: return to write our own xsd file
@bean: our wsdl objects injected into the spring container where name is the path we url to access, so the path employee.wsdl of my visit is http: // localhost: 9999 / cms / employee.wsdl
5. Writing the terminal
package com.example.webservice.controller;
import com.example.webservice.entity.Employee;
import com.example.webservice.entity.EmployeeDetailRequest;
import com.example.webservice.entity.EmployeeDetailResponse;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
@Endpoint
public class EmploySoapController {
private static final String NAMESPACE_URI="http://www.definesys.com/xml/employee";
@PayloadRoot(namespace = NAMESPACE_URI,localPart = "EmployeeDetailRequest2")
@ResponsePayload
public EmployeeDetailResponse getEmployee1(@RequestPayload EmployeeDetailRequest request){
EmployeeDetailResponse response=new EmployeeDetailResponse();
Employee employee=new Employee();
employee.setCode(request.getCode());
employee.setName(request.getName());
employee.setEmail(request.getEmail());
response.setEmployee(employee);
return response;
}
@PayloadRoot(namespace = NAMESPACE_URI,localPart = "EmployeeDetailRequest")
public EmployeeDetailResponse getEmployee2(@RequestPayload EmployeeDetailRequest request){
EmployeeDetailResponse response=new EmployeeDetailResponse();
Employee employee=new Employee();
employee.setCode(request.getCode());
employee.setName("LCH");
employee.setEmail("[email protected]");
response.setEmployee(employee);
return response;
}
}
@PayloadRoot: namespace we specified namespace, the client and server need to be consistent. localPart: Interface name to access the wsdl, the name of the same needs and the wsdl interface, localPart client access must also be consistent with the wsdl interface.
@ResponsePayload description of the output is a soap message, and somewhat similar @ResponseBody
Description @RequesPayLoad a soap message is entered and the like RequestBody
6. Test
1. Download and install soapui
2. Create a soap project
3. Add a wsdl file
4. Enter the path wsdl, my path is the following figure, if the error can go see if you can open the browser, you can not check the path is correct
http: // localhost: 9999 / cms / employee.wsdl. It did not change the default port 8080 friends
5. Enter the content request, the output is the output of the respective logical, because I have the xsd file changes
6. Client Access: url path to fill wsdl, localpart fill: the corresponding interface to access localPart