【问题标题】:NullPointer when soap message invokes web service肥皂消息调用 Web 服务时的空指针
【发布时间】:2016-03-07 12:44:12
【问题描述】:

尝试使用 spring 集成 ws 使用 web 服务,在 web 服务端我得到一个空指针,因为似乎传递的对象没有被编组或未映射到 xml,下面是调用调用的客户端的 sn-p服务 。

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

WSDL 由 JAxWS 端点类生成

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

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

不幸的是,stackoverflow 无法正确格式化 wsdl,但命名空间 id 是基于包名com.johnson4u,下面是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"/>

【问题讨论】:

    标签: java spring jax-ws spring-integration


    【解决方案1】:

    我认为字符串主体应该是

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

    没有包含命名空间符号“ns”

    【讨论】:

      【解决方案2】:

      我相信要取消编组对象,您需要在对象类中指定元素。

      public class Pojo {
          @XmlElement(name="pojoId", required=true, namespace=YOUR_NAME_SPACE)
          private int pojoId;
          @XmlElement(name="pojoName", required=true, namespace=YOUR_NAME_SPACE)
          private String pojoName;
      
          // Getters and Setters ......
      }
      

      【讨论】:

        【解决方案3】:

        我将 web 参数值更改为

          @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");
         }
            }
        

        和xml请求到

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-29
          • 2019-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多