Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use SOAP 1.2 with WebServiceGatewaySupport in Spring

I am very new to Spring framework and i had some problem to create a simple SOAP client with Spring.

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/

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

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;
}

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

A NullPointerException because my object messageFactory is NULL....

So the question is, how i can change to SOAP 1.2 the good way? Thanks for your answers.

like image 351
Siick Avatar asked Dec 24 '22 09:12

Siick


1 Answers

So i have finally found a way to make my call with SOAP 1.2 protocol.

Here is the full code of 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;


}
}
like image 161
Siick Avatar answered Dec 28 '22 13:12

Siick