2016-03-07 71 views
0

嘗試使用spring集成ws來使用webservice,在web服務端我得到一個空指針,因爲它似乎傳遞的對象沒有被編組或未映射在xml中,下面是客戶端調用調用該服務。當soap消息調用web服務時的空指針

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

可惜genrated中,wsdl正確的StackOverflow着的格式,但名稱空間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"/> 

回答

0

我改變了網絡參數值的方式來

@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>"; 
1

我相信對象是非編組的,你需要指定對象類中的元素。

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 ...... 
} 
2

我相信串身體要

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

命名空間的符號 'NS' 沒有被列入