Spring Boot calling SOAP Web Service

Spring Boot project, call the legacy SOAP Web Service, the method is very simple, just the introduction of spring-boot-starter-web-services.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

WebServiceTemplate

We used to call WebServiceTemplate SOAP Service. WebServiceTemplate provides three types of call methods sendSourceAndReceive, marshalSendAndReceive, sendAndReceive. sendSourceAndReceive direct method for transmitting and receiving XML message; marshalSendAndReceive transmission method, a reception result compared to the object, by the configuration of the automatic conversion and Marshaller Unmarshaller; sendAndReceive support lower-level operations.

package org.iata.caims.service.ws;

import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
import org.springframework.stereotype.Service;
import org.springframework.ws.client.core.WebServiceTemplate;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;

@Service
public class MyService {
    private static final String DEFAULT_URI = "http://localhost:8080/HelloService";
    private static final String MESSAGE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<ns1:sayHello xmlns:ns1=\"http://webservice.itrunner.org\">\n" +
            "   <ns1:in0>COCO</ns1:in0>\n" +
            "</ns1:sayHello>";

    private final WebServiceTemplate webServiceTemplate;

    public MyService(WebServiceTemplateBuilder webServiceTemplateBuilder) {
        this.webServiceTemplate = webServiceTemplateBuilder.setDefaultUri(DEFAULT_URI).build();
    }

    public void sendSourceAndReceive() {
        StreamSource source = new StreamSource(new StringReader(MESSAGE));
        StreamResult result = new StreamResult(System.out);
        webServiceTemplate.sendSourceAndReceiveToResult(source, result);
    }

    public Object marshalSendAndReceive(String uri, Object requestPayload) {
        return this.webServiceTemplate.marshalSendAndReceive(uri, requestPayload);
    }
}

marshalSendAndReceive is commonly used method. You can learn by reading wsdl file for the Web Service supports the methods and parameters, you can use SoapUI tool to generate the request, response XML, SOAP Domain objects and then write by hand. Simpler approach is to use maven-jaxb2-plugin widget generated automatically.

maven-jaxb2-plugin

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.14.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <generatePackage>org.itrunner.ws</generatePackage>
        <generateDirectory>${project.basedir}/src/main/java</generateDirectory>
        <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
        <schemaIncludes>
            <include>*.wsdl</include>
        </schemaIncludes>
    </configuration>
</plugin>

As configured, the wsdl file into resources / wsdl folder, will generate all Web Service methods related stub classes in the specified package maven compile time, including package-info.java, ObjectFactory, request, response, and has been configured XML annotation.
package-info.java

@javax.xml.bind.annotation.XmlSchema(namespace = "http://webservice.itrunner.org", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.iata.caims.service.test;

ObjectFactory

import javax.xml.bind.annotation.XmlRegistry;

@XmlRegistry
public class ObjectFactory {

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.itrunner.ws
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link SayHello }
     */
    public SayHello createSayHello() {
        return new SayHello();
    }

    /**
     * Create an instance of {@link SayHelloResponse }
     */
    public SayHelloResponse createSayHelloResponse() {
        return new SayHelloResponse();
    }
}

Request
Request class name corresponding to the Web Service method name, attributes corresponding to Web Service parameters.

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "in0"
})
@XmlRootElement(name = "sayHello")
public class SayHello {

    @XmlElement(required = true, nillable = true)
    protected String in0;

    /**
     * Gets the value of the in0 property.
     *
     * @return possible object is {@link String }
     */
    public String getIn0() {
        return in0;
    }

    /**
     * Sets the value of the in0 property.
     *
     * @param value allowed object is {@link String }
     */
    public void setIn0(String value) {
        this.in0 = value;
    }

}

Response

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "out"
})
@XmlRootElement(name = "sayHelloResponse")
public class SayHelloResponse {

    @XmlElement(required = true, nillable = true)
    protected String out;

    /**
     * Gets the value of the out property.
     *
     * @return possible object is {@link String }
     */
    public String getOut() {
        return out;
    }

    /**
     * Sets the value of the out property.
     *
     * @param value allowed object is {@link String }
     */
    public void setOut(String value) {
        this.out = value;
    }
}

Chopper small scale

Configuring WebServiceTemplateBuilde and Jaxb2Marshaller:

import org.springframework.boot.webservices.client.HttpWebServiceMessageSenderBuilder;
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.client.core.WebServiceTemplate;

import static java.time.Duration.ofSeconds;

@Configuration
public class Config {
    @Bean
    public WebServiceTemplate webServiceTemplate(WebServiceTemplateBuilder builder) {
        return builder.messageSenders(new HttpWebServiceMessageSenderBuilder().setConnectTimeout(ofSeconds(60)).setReadTimeout(ofSeconds(60)).build()).build();
    }

    @Bean
    public Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("org.itrunner.ws");
        return marshaller;
    }
}

Call the Web Service:

import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.stereotype.Service;
import org.springframework.ws.client.core.WebServiceTemplate;

@Service
public class HelloService {
    private static final String DEFAULT_URI = "http://localhost:8080/HelloService";

    private final WebServiceTemplate webServiceTemplate;

    public HelloService(WebServiceTemplateBuilder webServiceTemplateBuilder, Jaxb2Marshaller jaxb2Marshaller) {
        this.webServiceTemplate = webServiceTemplateBuilder.setDefaultUri(DEFAULT_URI)
                .setMarshaller(jaxb2Marshaller).setUnmarshaller(jaxb2Marshaller).build();
    }

    public SayHelloResponse sayHello(SayHello request) {
        return (SayHelloResponse) this.webServiceTemplate.marshalSendAndReceive(request);
    }
}

Reference Documents

Spring Boot Reference Guide
Spring Boot SOAP Client – WebServiceTemplate Example

Guess you like

Origin blog.51cto.com/7308310/2415052