我有一个spring-boot应用程序,我需要发送SOAP-Message(版本1.2)并收到回复。超时应该是30秒;
以下是我的课程;
@Component
public class SettingsSenderClient extends WebServiceGatewaySupport {
@Autowired
private DatabaseService databaseService;
@Autowired
public void setHttpComponentsMessageSender(HttpComponentsMessageSenderWithTimeout messageSenderWithTimeout) {
WebServiceTemplate template = getWebServiceTemplate();
template.setMessageSender(messageSenderWithTimeout);
//template.setMessageFactory(messageFactory());
}
//@Bean(name = "webServiceTemplate")
//public WebServiceTemplate webServiceTemplate() {
//WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
//webServiceTemplate.setMessageSender(httpComponentsMessageSender());
//webServiceTemplate.setMessageFactory(messageFactory());
//return webServiceTemplate;
//}
@Bean
public SaajSoapMessageFactory messageFactory() {
SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
messageFactory.setSoapVersion(SoapVersion.SOAP_12);
return messageFactory;
}
public void sendSettings() {
List<SettingsDetails> settingsDetails = databaseService.getSettingsDetailBySettingsID();
if (!settingsDetails.isEmpty()) {
//Build the "SettingsRequest" SOAP Message From WSDL Generated Classes For Each Record
settingsDetails.stream().forEachOrdered((settingsDetail) -> {
try {
//Build SettingsDetail Request
SettingsDetailRequest request = new SettingsDetailRequest();
request.setDetail1(settingsDetail.getSettingsDetail1());
request.setDetail2(settingsDetail.getSettingsDetail2());
//Log The URL To Send
String settingURL = settingsDetail.getSettingsURL();
LogService.info(this.getClass().getName(),"Sending Message on URL: " + deviceURL);
JAXBElement<SettingsDetailResponse> jaxbElementResponse = (JAXBElement<SettingsDetailResponse>) getWebServiceTemplate()
.marshalSendAndReceive(settingURL,
new ObjectFactory().createSettingsDetailRequest(request), (WebServiceMessage message) -> {
SaajSoapMessage soapMessage = (SaajSoapMessage) message;
SoapHeaderElement id = soapMessage.getSoapHeader().addHeaderElement(new QName(namespaceUrl, localpart, "sp"));
id.setText(settingsDetail.getSettingName());
});
SettingsDetailResponse response = jaxbElementResponse.getValue();
//Get The Response Status
String responseStatus = response.getStatus().value();
//Wait 1 Second in each loop
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException ex) {
LogService.error(this.getClass().getName(), "Intrp Error");
LogService.error(this.getClass().getName(), ex);
}
} catch (WebServiceIOException ex) {
LogService.error(this.getClass().getName(), ex.getMessage());
//Catch The Timeout Exception Here (How??? //TODO)
LogService.error(this.getClass().getName(), ex);
}
});
} else {
LogService.info(this.getClass().getName(), "No Records Found To Send Settings SOAP Message(s)");
}
}
}
我将messageFactory
bean配置为使用SoapVersion.SOAP_12
。
但我仍然得到错误:
[Exception: ]org.springframework.ws.soap.SoapMessageCreationException: Could not create message from InputStream: Unable to internalize message; nested exception is com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to internalize message
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:216)
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:60)
at org.springframework.ws.transport.AbstractWebServiceConnection.receive(AbstractWebServiceConnection.java:92)
at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:611)
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555)
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390)
at com.myapp.ws.outgoing.SettingsSenderJobClient.lambda$sendSettings$1(SettingsSenderJobClient.java:107)
at com.myapp.ws.outgoing.SettingsSenderJobClient$$Lambda$8/1215330412.accept(Unknown Source)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source)
at java.util.stream.ReferencePipeline$Head.forEachOrdered(Unknown Source)
at com.myapp.ws.outgoing.SettingsSenderJobClient.sendSettings(SettingsSenderJobClient.java:71)
at com.tingco.myapp.SettingsSenderJob.execute(SettingsSenderJob.java:24)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to internalize message
at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.init(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.<init>(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.ver1_1.Message1_1Impl.<init>(Unknown Source)
at com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl.createMessage(Unknown Source)
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:188)
... 13 more
Caused by: com.sun.xml.internal.messaging.saaj.soap.SOAPVersionMismatchException: Cannot create message: incorrect content-type for SOAP version. Got: application/soap+xml; charset=utf-8; action="/ChangeConfiguration" Expected: text/xml
... 18 more
更新
我删除了SaajSoapMessageFactory
,并且确实如下所示。这有什么区别?他们俩都不是messageFactories
?
@Bean
public WebServiceMessageFactory webServiceMessageFactory() throws SOAPException {
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
WebServiceMessageFactory webMessageFactory = new SaajSoapMessageFactory(messageFactory);
return webMessageFactory;
}
@Autowired
public void setHttpComponentsMessageSender(HttpComponentsMessageSenderWithTimeout messageSenderWithTimeout) throws SOAPException {
WebServiceTemplate template = getWebServiceTemplate();
template.setMessageSender(messageSenderWithTimeout);
template.setMessageFactory(webServiceMessageFactory());
}
如果我对SaajSoapMessageFactory
执行相同操作,则会获得nullpointer exception
。