问题描述
我对 Spring 框架非常陌生,在使用 Spring 创建一个简单的 SOAP 客户端时遇到了一些问题.
I am very new to Spring framework and i had some problem to create a simple SOAP client with Spring.
像一个优秀的新手一样,我使用 Spring 教程制作了我的 SOAP 客户端.你可以在这里找到它 -> https://spring.io/guides/gs/消费网络服务/
Like a good newbie, i have used the Spring tutorial to make my SOAP client. You can find it here -> https://spring.io/guides/gs/consuming-web-service/
使用示例中给出的 Web 服务进行测试是可以的.我已经更改了代码以使其与我的网络服务一起使用,所以这是文件:
The test with the web service given in example is OK. I have changed the code to make it work with my web service, so this is the files :
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());
};
}
}
但是当我启动应用程序时出现错误
but i have an error when i laucnh the app
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
So it seems i need to change the protocol for SOAP 1.2 but i don't find the right way to do it. I have already tested this solution in 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,但我在
but if i do that, it changes the protocal to SOAP 1.2 but i have this error after
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....
A NullPointerException because my object messageFactory is NULL....
所以问题是,我如何才能以好的方式更改为 SOAP 1.2?感谢您的回答.
So the question is, how i can change to SOAP 1.2 the good way?Thanks for your answers.
推荐答案
所以我终于找到了一种使用 SOAP 1.2 协议进行调用的方法.
So i have finally found a way to make my call with SOAP 1.2 protocol.
这里是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;
}
}
这篇关于在 Spring 中使用带有 WebServiceGatewaySupport 的 SOAP 1.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!