在春季将 SOAP 1.2 与 WebServiceGatewaySupport 结合使用



我对Spring框架很陌生,使用Spring创建一个简单的SOAP客户端时遇到了一些问题。

像一个优秀的新手一样,我使用Spring教程来制作我的SOAP客户端。你可以在这里找到它 -> https://spring.io/guides/gs/consuming-web-service/

示例中给出的 Web 服务的测试是可以的。我已经更改了代码以使其与我的 Web 服务一起使用,所以这是文件:

SetCodePinConfigurartion.java

package hello;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@Configuration
public class SetPinCodeConfiguration {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
// this package must match the package in the <generatePackage> specified in
// pom.xml
marshaller.setContextPath("hello.wsdl");
return marshaller;
}
@Bean
public SetPinCodeClient SetPinCodeClient(Jaxb2Marshaller marshaller) {
SetPinCodeClient client = new SetPinCodeClient();
client.setDefaultUri("http://xxx.xxx.xx.x/PlayerServices/PlayerIntelligence/PlayerManagementService");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}

SetPinCodeClient.java

package hello;
import javax.xml.bind.JAXBElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;
import hello.wsdl.SetPinCode;
import hello.wsdl.SetPinCodeResponse;
public class SetPinCodeClient extends WebServiceGatewaySupport{
private static final Logger log = LoggerFactory.getLogger(SetPinCodeClient.class);
public SetPinCodeResponse SetPinCode(JAXBElement<String> pinCode, JAXBElement<String> cardNumber, JAXBElement<Integer> casinoCreationId) {
SetPinCode request = new SetPinCode();
request.setPinCode(pinCode);
request.setCardNumber(cardNumber);
request.setCasinoCreationId(casinoCreationId);
log.info("Requesting save in database for card " + cardNumber + "with pin code" + pinCode + "from casino n°" + casinoCreationId);
SetPinCodeResponse response = (SetPinCodeResponse) getWebServiceTemplate()
.marshalSendAndReceive("http://xxx.xxx.xx.x/PlayerServices/PlayerIntelligence/PlayerManagementService",
request,
new SoapActionCallback("http://xxx.xxx.xx.x/PlayerServices/PlayerIntelligence/PlayerManagementService/SetPinCode"));
return response;
}
}

应用.java

package hello;
import javax.xml.bind.JAXBElement;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import hello.wsdl.ObjectFactory;
import hello.wsdl.SetPinCodeResponse;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
CommandLineRunner lookup(SetPinCodeClient setPinCodeClient) {
return args -> {
JAXBElement<String> pinCode = new ObjectFactory().createSetPinCodeCardNumber("2004");
JAXBElement<String> cardNumber = new ObjectFactory().createSetPinCodeCardNumber("B09B036");
JAXBElement<Integer> casinoCreationId = new ObjectFactory().createSetPinCodeCasinoCreationId(185);
SetPinCodeResponse response = setPinCodeClient.SetPinCode(pinCode, cardNumber, casinoCreationId);
System.err.println(response.toString());
};
}
}

但是当我使用应用程序时我遇到了错误

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:793) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:774) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at hello.Application.main(Application.java:18) [classes/:na]
Caused by: org.springframework.ws.client.WebServiceTransportException: Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. [415]
at org.springframework.ws.client.core.WebServiceTemplate.handleError(WebServiceTemplate.java:699) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:609) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at hello.SetPinCodeClient.SetPinCode(SetPinCodeClient.java:33) ~[classes/:na]
at hello.Application.lambda$0(Application.java:28) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:790) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
... 5 common frames omitted

所以似乎我需要更改 SOAP 1.2 的协议,但我找不到正确的方法。我已经在SetPinCodeClient中测试了这个解决方案.java

public SetPinCodeResponse SetPinCode(JAXBElement<String> pinCode, JAXBElement<String> cardNumber, JAXBElement<Integer> casinoCreationId) {
SetPinCode request = new SetPinCode();
request.setPinCode(pinCode);
request.setCardNumber(cardNumber);
request.setCasinoCreationId(casinoCreationId);
log.info("Requesting save in database for card " + cardNumber + "with pin code" + pinCode + "from casino n°" + casinoCreationId);
SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
messageFactory.setSoapVersion(SoapVersion.SOAP_12);
WebServiceTemplate wsTemplate = getWebServiceTemplate();
wsTemplate.setMessageFactory(messageFactory);
SetPinCodeResponse response = (SetPinCodeResponse) wsTemplate
.marshalSendAndReceive("http://192.168.67.63:8095/PlayerServices/PlayerIntelligence/PlayerManagementService",
request,
new SoapActionCallback("http://192.168.67.63:8095/PlayerServices/PlayerIntelligence/PlayerManagementService/SetPinCode"));
return response;
}

但是如果我这样做,它会将原型更改为 SOAP 1.2,但在之后出现此错误

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:793) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:774) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at hello.Application.main(Application.java:18) [classes/:na]
Caused by: java.lang.NullPointerException: null
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:174) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:60) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.context.DefaultMessageContext.<init>(DefaultMessageContext.java:42) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:553) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at hello.SetPinCodeClient.SetPinCode(SetPinCodeClient.java:36) ~[classes/:na]
at hello.Application.lambda$0(Application.java:28) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:790) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
... 5 common frames omitted

一个 NullPointerException,因为我的对象消息工厂是 NULL...。

所以问题是,我怎样才能以好的方式更改为 SOAP 1.2? 感谢您的回答。

所以我终于找到了一种使用 SOAP 1.2 协议进行调用的方法。

以下是SetPinCodeClient的完整代码.java

package hello;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapHeaderElement;
import org.springframework.ws.soap.client.core.SoapActionCallback;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import hello.wsdl.SetPinCode;
import hello.wsdl.SetPinCodeResponse;
public class SetPinCodeClient extends WebServiceGatewaySupport{
private static final Logger log = LoggerFactory.getLogger(SetPinCodeClient.class);
public SetPinCodeResponse SetPinCode(JAXBElement<String> pinCode, JAXBElement<String> cardNumber, JAXBElement<Integer> casinoCreationId) throws SOAPException {
String action ="http://tempuri.org/Player_x0020_Management_x0020_Service/SetPinCode";
String uri = "http://xxx.xxx.xx.x/PlayerServices/PlayerIntelligence/PlayerManagementService";
SetPinCode request = new SetPinCode();
request.setPinCode(pinCode);
request.setCardNumber(cardNumber);
request.setCasinoCreationId(casinoCreationId);
log.info("Requesting save in database for card " + cardNumber + "with pin code" + pinCode + "from casino n°" + casinoCreationId);
MessageFactory msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SaajSoapMessageFactory saajSoapMessageFactory = new SaajSoapMessageFactory(msgFactory);
WebServiceTemplate wsTemplate = getWebServiceTemplate();
wsTemplate.setMessageFactory(saajSoapMessageFactory);
SoapActionCallback requestCallback = new SoapActionCallback(action) {
public void doWithMessage(WebServiceMessage message) {
SaajSoapMessage soapMessage = (SaajSoapMessage) message;
SoapHeader soapHeader = soapMessage.getSoapHeader();
QName wsaToQName = new QName("http://www.w3.org/2005/08/addressing", "To", "wsa");
SoapHeaderElement wsaTo =  soapHeader.addHeaderElement(wsaToQName);
wsaTo.setText(uri);
QName wsaActionQName = new QName("http://www.w3.org/2005/08/addressing", "Action", "wsa");
SoapHeaderElement wsaAction =  soapHeader.addHeaderElement(wsaActionQName);
wsaAction.setText(action);
}
};
SetPinCodeResponse response = (SetPinCodeResponse) wsTemplate.marshalSendAndReceive(uri, request, requestCallback);
return response;

}
}

还有一种更简单的方法来启用 SOAP 1.2 支持,它类似于为客户端设置编组器的方式。只需使用正确的 SOAP 版本创建一个WebServiceMessageFactoryBean:

@Bean
WebServiceMessageFactory messageFactory()
{
SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
messageFactory.setSoapVersion(SoapVersion.SOAP_12);
return messageFactory;
}

你只需要将上面的 bean 添加到你的配置类中,把它作为一个参数传递给你的客户端 bean,然后调用setMessageFactory(..)

@Configuration
public class SetPinCodeConfiguration
{
@Bean
public Jaxb2Marshaller marshaller()
{
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
// this package must match the package in the <generatePackage> specified in
// pom.xml
marshaller.setContextPath("hello.wsdl");
return marshaller;
}
@Bean
public SetPinCodeClient SetPinCodeClient(Jaxb2Marshaller marshaller, WebServiceMessageFactory messageFactory)
{
SetPinCodeClient client = new SetPinCodeClient();
client.setDefaultUri("http://xxx.xxx.xx.x/PlayerServices/PlayerIntelligence/PlayerManagementService");
client.setMessageFactory(messageFactory); // <-- set the message factory
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}

相关内容

  • 没有找到相关文章

最新更新