Search code examples
javaspringjax-wsspring-integration

NullPointer when soap message invokes web service


Trying to consume a webservice using spring integration ws, On the webservice end i get a null pointer as it seems the object passed isnt been marshalled or not mapped in the xml, Below is the snippet of the client calling invoking the service .

public class Main {

public static void main(String[] args) {
    ClassPathXmlApplicationContext context
            = new ClassPathXmlApplicationContext("springws.xml");
    MessageChannel channel = context.getBean("request", MessageChannel.class);

      String body = "<getPojo xmlns=\"http://johnson4u.com/\"><pojo>\n"
            + "    <pojoId>23</pojoId>\n"
            + "    <pojoName>dubic</pojoName>\n"
            + "</pojo></getPojo>";

    MessagingTemplate messagingTemplate = new MessagingTemplate();
    Message<?> message = messagingTemplate.sendAndReceive(
            channel, MessageBuilder.withPayload(body).build());

    System.out.println(message.getPayload());
}

The WSDL is genrated by the JAxWS endpoint class

  package com.johnson4u;


  import javax.jws.WebService;
  import javax.jws.WebMethod;
  import javax.jws.WebParam;


@WebService(serviceName = "SpringService")
public class SpringService {


@WebMethod(operationName = "getPojo" )
public Pojo getPojo(@WebParam(Pojo pjRequest){
    //Null Pointer occurs here as pjRequest might not be mapped to xml

   System.out.println("Pojo name is "+pjRequest.getPojoName());

   return new Pojo(234,"IM new Pojo");
}

And the POJO

  package com.johnson4u;


public class Pojo {

private int pojoId;
private String pojoName;

public Pojo(int pojoId, String pojoName) {
    this.pojoId = pojoId;
    this.pojoName = pojoName;
}


public int getPojoId() {
    return pojoId;
}

public void setPojoId(int pojoId) {
    this.pojoId = pojoId;
}

public String getPojoName() {
    return pojoName;
}

public void setPojoName(String pojoName) {
    this.pojoName = pojoName;
}

Unfortunately,stackoverflow cant format the wsdl properly, but the namespace id are based on the package name com.johnson4u, below is spring-ws-context.xml

<int:channel   id="request" />      
<int:channel id="response" />

<ws:outbound-gateway  id="gateway"   
                      request-channel="request"   
                      uri="http://localhost:20151/SpringWs/SpringService?wsdl"/>


Solution

  • I changed web param value to

      @WebMethod(operationName = "getPojo" )
       public Pojo getPojo(@WebParam(name = "pojo") Pojo pjRequest){
    
      System.out.println("Pojo name is "+pjRequest.getPojoName());
    
       return new Pojo(234,"IM new Pojo");
     }
        }
    

    and xml request to

           String body = "<ns0:getPojo    xmlns:ns0=\"http://johnson4u.com/\">\n" +
     "               <pojo>"
                 + "<pojoId>456</pojoId>"
                  +"<pojoName>Johnson</pojoName>"
                    + "</pojo>\n" +
           "        </ns0:getPojo>";