gpt4 book ai didi

java - 如何使用 WebServiceTemplate 发送 SOAP 请求?

转载 作者:IT老高 更新时间:2023-10-28 13:49:20 24 4
gpt4 key购买 nike

我正在尝试向 SOAP 网络服务发送请求。我读了this tutorial并准备了以下代码。但是,我将向多个 SOAP Web 服务发送不同的请求,而本教程只关注一个请求。如何使用 WebserviceTemplate 发送 SOAP 请求?

WebServiceTemplate

    SoapMessage soapMsg = new SoapMessage();
soapMsg.setUsername("Requester");
soapMsg.setPassword("Pass");
soapMsg.setLanguageCode("EN");
Request request = new Request();
request.setDeparture("FDH");
request.setDestination("HAM");
Date date = new Date();
SimpleDateFormat frm2 = new SimpleDateFormat("yyyy-MM-dd");
request.setDepartureDate(frm2.parse(frm2.format(date)));
request.setNumADT(1);
request.setNumCHD(0);
request.setNumInf(0);
request.setCurrencyCode("EUR");
request.setWaitForResult(true);
request.setNearByDepartures(true);
request.setNearByDestinations(true);
request.setRronly(false);
request.setMetaSearch(false);
soapMsg.setRequest(request);
WebServiceTemplate webServiceTemplate = new WebServiceTemplate(). //how to create object and send request!
Object response = webServiceTemplate.marshalSendAndReceive(
"https://aaa5.elsyarres.net", soapMsg);
Response msg = (Response) response;
System.err.println("size of results of wogolo:"
+ msg.getFlights().getFlight().size());

最佳答案

你可以使用下面的代码,你不需要在xml文件中定义任何东西。

  try {
SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory(
MessageFactory.newInstance());
messageFactory.afterPropertiesSet();

WebServiceTemplate webServiceTemplate = new WebServiceTemplate(
messageFactory);
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();

marshaller.setContextPath("PACKAGE");
marshaller.afterPropertiesSet();

webServiceTemplate.setMarshaller(marshaller);
webServiceTemplate.afterPropertiesSet();

Response response = (Response) webServiceTemplate
.marshalSendAndReceive(
"address",
searchFlights);

Response msg = (Response) response;
} catch (Exception s) {
s.printStackTrace();
}

关于java - 如何使用 WebServiceTemplate 发送 SOAP 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34368029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com