日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

使用Spring编写和使用SOAP Web服务

發(fā)布時(shí)間:2023/12/3 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Spring编写和使用SOAP Web服务 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在RESTful Web服務(wù)時(shí)代,我有機(jī)會(huì)使用SOAP Web Service。 為此,我選擇了Spring ,這是因?yàn)槲覀円呀?jīng)在項(xiàng)目中使用Spring作為后端框架,其次它提供了一種直觀的方式來(lái)與具有明確定義的邊界的服務(wù)進(jìn)行交互,以通過(guò)WebServiceTemplate促進(jìn)可重用性和可移植性。

假設(shè)您已經(jīng)了解SOAP Web服務(wù),讓我們開(kāi)始創(chuàng)建在端口9999上運(yùn)行的hello-world soap服務(wù),并使用下面的步驟來(lái)使用相同的客戶端:

步驟1 :根據(jù)以下圖像,轉(zhuǎn)到start.spring.io并創(chuàng)建一個(gè)添加Web啟動(dòng)器的新項(xiàng)目soap-server

步驟2:編輯SoapServerApplication.java,以在端點(diǎn)發(fā)布hello-world服務(wù)-http:// localhost:9999 / service / hello-world ,如下所示:

package com.arpit.soap.server.main;import javax.xml.ws.Endpoint;import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;import com.arpit.soap.server.service.impl.HelloWorldServiceImpl;@SpringBootApplication public class SoapServerApplication implements CommandLineRunner {@Value("${service.port}")private String servicePort;@Overridepublic void run(String... args) throws Exception {Endpoint.publish("http://localhost:" + servicePort+ "/service/hello-world", new HelloWorldServiceImpl());}public static void main(String[] args) {SpringApplication.run(SoapServerApplication.class, args);} }

步驟3:編輯application.properties,以指定hello-world服務(wù)的應(yīng)用程序名稱,端口和端口號(hào),如下所示:

server.port=9000 spring.application.name=soap-server## Soap Service Port service.port=9999

步驟4:創(chuàng)建其他包com.arpit.soap.server.servicecom.arpit.soap.server.service.impl來(lái)定義Web Service及其實(shí)現(xiàn),如下所示:

HelloWorldService.java

package com.arpit.soap.server.service;import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService;import com.arpit.soap.server.model.ApplicationCredentials;@WebService public interface HelloWorldService {@WebMethod(operationName = "helloWorld", action = "https://aggarwalarpit.wordpress.com/hello-world/helloWorld")String helloWorld(final String name,@WebParam(header = true) final ApplicationCredentials credential);}

上面指定的@WebService將Java類標(biāo)記為實(shí)現(xiàn)Web服務(wù),或者將Java接口標(biāo)記為定義Web Service接口。

上面指定的@WebMethod將Java方法標(biāo)記為Web Service操作。

上面指定的@WebParam自定義單個(gè)參數(shù)到Web服務(wù)消息部分和XML元素的映射。

HelloWorldServiceImpl.java

package com.arpit.soap.server.service.impl;import javax.jws.WebService;import com.arpit.soap.server.model.ApplicationCredentials; import com.arpit.soap.server.service.HelloWorldService;@WebService(endpointInterface = "com.arpit.soap.server.service.HelloWorldService") public class HelloWorldServiceImpl implements HelloWorldService {@Overridepublic String helloWorld(final String name,final ApplicationCredentials credential) {return "Hello World from " + name;} }

步驟5:移至soap-server目錄并運(yùn)行命令: mvn spring-boot:run 。 運(yùn)行后,打開(kāi)http:// localhost:9999 / service / hello-world?wsdl以查看hello-world服務(wù)的WSDL。

接下來(lái),我們將創(chuàng)建soap-client ,它將使用我們新創(chuàng)建的hello-world服務(wù)。

步驟6:轉(zhuǎn)到start.spring.io并基于下圖創(chuàng)建一個(gè)新的項(xiàng)目soap-client,添加Web,Web Services啟動(dòng)程序:

步驟7:編輯SoapClientApplication.java以創(chuàng)建一個(gè)向hello-world Web服務(wù)的請(qǐng)求,將該請(qǐng)求連同標(biāo)頭一起發(fā)送到soap-server并從中獲取響應(yīng),如下所示:

package com.arpit.soap.client.main;import java.io.IOException; import java.io.StringWriter;import javax.xml.bind.JAXBElement; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.ws.WebServiceMessage; import org.springframework.ws.client.core.WebServiceMessageCallback; import org.springframework.ws.client.core.WebServiceTemplate; import org.springframework.ws.soap.SoapMessage; import org.springframework.xml.transform.StringSource;import com.arpit.soap.server.service.ApplicationCredentials; import com.arpit.soap.server.service.HelloWorld; import com.arpit.soap.server.service.HelloWorldResponse; import com.arpit.soap.server.service.ObjectFactory;@SpringBootApplication @ComponentScan("com.arpit.soap.client.config") public class SoapClientApplication implements CommandLineRunner {@Autowired@Qualifier("webServiceTemplate")private WebServiceTemplate webServiceTemplate;@Value("#{'${service.soap.action}'}")private String serviceSoapAction;@Value("#{'${service.user.id}'}")private String serviceUserId;@Value("#{'${service.user.password}'}")private String serviceUserPassword;public static void main(String[] args) {SpringApplication.run(SoapClientApplication.class, args);System.exit(0);}public void run(String... args) throws Exception {final HelloWorld helloWorld = createHelloWorldRequest();@SuppressWarnings("unchecked")final JAXBElement<HelloWorldResponse> jaxbElement = (JAXBElement<HelloWorldResponse>) sendAndRecieve(helloWorld);final HelloWorldResponse helloWorldResponse = jaxbElement.getValue();System.out.println(helloWorldResponse.getReturn());}private Object sendAndRecieve(HelloWorld seatMapRequestType) {return webServiceTemplate.marshalSendAndReceive(seatMapRequestType,new WebServiceMessageCallback() {public void doWithMessage(WebServiceMessage message)throws IOException, TransformerException {SoapMessage soapMessage = (SoapMessage) message;soapMessage.setSoapAction(serviceSoapAction);org.springframework.ws.soap.SoapHeader soapheader = soapMessage.getSoapHeader();final StringWriter out = new StringWriter();webServiceTemplate.getMarshaller().marshal(getHeader(serviceUserId, serviceUserPassword),new StreamResult(out));Transformer transformer = TransformerFactory.newInstance().newTransformer();transformer.transform(new StringSource(out.toString()),soapheader.getResult());}});}private Object getHeader(final String userId, final String password) {final https.aggarwalarpit_wordpress.ObjectFactory headerObjectFactory = new https.aggarwalarpit_wordpress.ObjectFactory();final ApplicationCredentials applicationCredentials = new ApplicationCredentials();applicationCredentials.setUserId(userId);applicationCredentials.setPassword(password);final JAXBElement<ApplicationCredentials> header = headerObjectFactory.createApplicationCredentials(applicationCredentials);return header;}private HelloWorld createHelloWorldRequest() {final ObjectFactory objectFactory = new ObjectFactory();final HelloWorld helloWorld = objectFactory.createHelloWorld();helloWorld.setArg0("Arpit");return helloWorld;}}

步驟8:接下來(lái),創(chuàng)建其他包com.arpit.soap.client.config來(lái)配置WebServiceTemplate ,如下所示:

ApplicationConfig.java

package com.arpit.soap.client.config;import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.oxm.jaxb.Jaxb2Marshaller; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.ws.client.core.WebServiceTemplate; import org.springframework.ws.soap.saaj.SaajSoapMessageFactory; import org.springframework.ws.transport.http.HttpComponentsMessageSender;@Configuration @EnableWebMvc public class ApplicationConfig extends WebMvcConfigurerAdapter {@Value("#{'${service.endpoint}'}")private String serviceEndpoint;@Value("#{'${marshaller.packages.to.scan}'}")private String marshallerPackagesToScan;@Value("#{'${unmarshaller.packages.to.scan}'}")private String unmarshallerPackagesToScan;@Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}@Beanpublic SaajSoapMessageFactory messageFactory() {SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();messageFactory.afterPropertiesSet();return messageFactory;}@Beanpublic Jaxb2Marshaller marshaller() {Jaxb2Marshaller marshaller = new Jaxb2Marshaller();marshaller.setPackagesToScan(marshallerPackagesToScan.split(","));return marshaller;}@Beanpublic Jaxb2Marshaller unmarshaller() {Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();unmarshaller.setPackagesToScan(unmarshallerPackagesToScan.split(","));return unmarshaller;}@Beanpublic WebServiceTemplate webServiceTemplate() {WebServiceTemplate webServiceTemplate = new WebServiceTemplate(messageFactory());webServiceTemplate.setMarshaller(marshaller());webServiceTemplate.setUnmarshaller(unmarshaller());webServiceTemplate.setMessageSender(messageSender());webServiceTemplate.setDefaultUri(serviceEndpoint);return webServiceTemplate;}@Beanpublic HttpComponentsMessageSender messageSender() {HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();return httpComponentsMessageSender;} }

步驟9:編輯application.properties以指定應(yīng)用程序名稱,端口和hello-world soap Web服務(wù)配置,如下所示:

server.port=9000 spring.application.name=soap-client## Soap Service Configurationservice.endpoint=http://localhost:9999/service/hello-world service.soap.action=https://aggarwalarpit.wordpress.com/hello-world/helloWorld service.user.id=arpit service.user.password=arpit marshaller.packages.to.scan=com.arpit.soap.server.service unmarshaller.packages.to.scan=com.arpit.soap.server.service

上面指定的service.endpoint是提供給服務(wù)用戶以調(diào)用服務(wù)提供者公開(kāi)的服務(wù)的URL。

service.soap.action指定服務(wù)請(qǐng)求者發(fā)送請(qǐng)求時(shí)需要調(diào)用哪個(gè)進(jìn)程或程序,還定義了進(jìn)程/程序的相對(duì)路徑。

marshaller.packages.to.scan指定在將請(qǐng)求發(fā)送到服務(wù)器之前在編組時(shí)要掃描的軟件包。

unmarshaller.packages.to.scan指定從服務(wù)器收到請(qǐng)求后在解組時(shí)要掃描的軟件包。

現(xiàn)在,我們將使用wsimport從WSDL生成Java對(duì)象,并將其復(fù)制到在終端上執(zhí)行以下命令的soap-client項(xiàng)目:

wsimport -keep -verbose http://localhost:9999/service/hello-world?wsdl

步驟10:移至soap-client目錄并運(yùn)行命令: mvn spring-boot:run 。 命令完成后,我們將在控制臺(tái)上看到“來(lái)自Arpit的Hello World”作為hello-world soap服務(wù)的響應(yīng)。

在運(yùn)行時(shí),如果遇到以下錯(cuò)誤,則– 由于缺少@XmlRootElement批注,因此無(wú)法封送鍵入“ com.arpit.soap.server.service.HelloWorld”作為元素,然后添加@XmlRootElement(name =“ helloWorld”,命名空間= “ http://service.server.soap.arpit.com/ ”到com.arpit.soap.server.service.HelloWorld ,其中名稱空間應(yīng)與在肥皂信封中定義的xmlns:ser相匹配,如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.server.soap.arpit.com/"><soapenv:Header><ser:arg1><userId>arpit</userId><password>arpit</password></ser:arg1></soapenv:Header><soapenv:Body><ser:helloWorld><!--Optional:--><arg0>Arpit</arg0></ser:helloWorld></soapenv:Body> </soapenv:Envelope>

完整的源代碼托管在github上 。

翻譯自: https://www.javacodegeeks.com/2016/07/writing-consuming-soap-webservice-spring.html

總結(jié)

以上是生活随笔為你收集整理的使用Spring编写和使用SOAP Web服务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。