我是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;
}
}
Application.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;
}
但是,如果我这样做,它会将protocal更改为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,因为我的对象messageFactory为NULL ....
所以问题是,我如何才能改进SOAP 1.2的好方法? 谢谢你的回答。
答案 0 :(得分:2)
还有一种更简单的方法来启用 SOAP 1.2 支持,这类似于为您的客户端设置编组器的方式。只需使用正确的 SOAP 版本创建一个 WebServiceMessageFactory
bean:
@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;
}
}
答案 1 :(得分:1)
所以我终于找到了一种使用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;
}
}