Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring Boot calls SOAP Web Service

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

In the Spring Boot project, the legacy SOAP Web Service is called, and the method is simple, as long as spring-boot-starter-web-services is introduced.

Org.springframework.boot spring-boot-starter-web-servicesWebServiceTemplate

We use WebServiceTemplate to call SOAP Service. WebServiceTemplate provides three types of calling methods sendSourceAndReceive, marshalSendAndReceive, and sendAndReceive. The sendSourceAndReceive method sends and receives the result directly, and the XML message;marshalSendAndReceive method sends and receives the result as an object, which is automatically converted by the configured Marshaller and Unmarshaller; sendAndReceive supports 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;@Servicepublic class MyService {private static final String DEFAULT_URI = "http://localhost:8080/HelloService";" Private static final String MESSAGE = "" + "\ n" + "COCO\ n" + "; 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 a common method. You can read the wsdl file to learn about the methods and parameters supported by Web Service, or you can use the SoapUI tool to generate request, response XML, and then manually write SOAP Domain objects. An easier way is to use the maven-jaxb2-plugin plug-in to generate it automatically.

Maven-jaxb2-plugin org.jvnet.jaxb2.maven2 maven-jaxb2-plugin 0.14.0 generate org.itrunner.ws ${project.basedir} / src/main/java ${project.basedir} / src/main/resources/wsdl * .wsdl

As configured above, put the wsdl file in the resources/wsdl folder, and when maven compiles, it will generate all the stub classes related to the Web Service method in the specified package, including package-info.java, ObjectFactory, request, response, and XML annotations have been configured.

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;@XmlRegistrypublic 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

The Request class name corresponds to the Web Service method name, and the property corresponds to the Web Service parameter.

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;}}

Configure 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 @ Configurationpublic 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 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;@Servicepublic 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 documentation

Spring Boot Reference Guide

Spring Boot SOAP Client-WebServiceTemplate Example

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report