2012-04-10 45 views
2

我有以下端點接口:JAX-WS:將返回圍繞一個ArrayList的包裝

@WebService 
public interface SEIWebService { 

    @WebMethod 
    @WebResult(name="CreateWorkOrderItemResponse") 
    CreateWorkOrderItemResponse createWorkItem(@WebParam(name = "CreateWorkOrderItemRequest")CreateWorkOrderItemRequest request); 
} 

實施:

@WebService(endpointInterface = "com.someCompany.SEIWebService", portName = "SEIWebServices") 
public class SEIWebServiceImpl implements SEIWebService{ 

    @Override 
    public CreateWorkOrderItemResponse createWorkItem(CreateWorkOrderItemRequest request) { 
     CreateWorkOrderItemResponse response = new CreateWorkOrderItemResponse(); 
     response.setResponseCode("Testing Create 2222"); 
     response.addError("Error 1"); 
     response.addError("Error 2"); 

     return response; 
    } 

最後,爲響應對象

代碼
public class CreateWorkOrderItemResponse { 
    private String responseCode = null; 
    private ArrayList<String> errorList = new ArrayList<String>(); 

    public void setResponseCode(String responseCode) { 
     this.responseCode = responseCode; 
    } 

    public String getResponseCode() { 
     return responseCode; 
    } 

    public void addError(String error) { 
     errorList.add(error); 
    } 

    public void setErrorList(ArrayList<String> errorList) { 
     this.errorList = errorList; 
    } 

    public ArrayList<String> getErrorList() { 
     return errorList; 
    } 
} 

當我運行這段代碼時,SoapUI中的響應如下所示:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <ns2:createWorkItemResponse xmlns:ns2="http://someCompany.com/"> 
     <CreateWorkOrderItemResponse> 
      <errorList>Error 1</errorList> 
      <errorList>Error 2</errorList> 
      <responseCode>Testing Create 2222</responseCode> 
      <testList/> 
     </CreateWorkOrderItemResponse> 
     </ns2:createWorkItemResponse> 
    </S:Body> 
</S:Envelope> 

最後,問題 ...通過上面的代碼,有沒有辦法改變它,所以我可以添加一個「包裝」,圍繞errorList反應?我期待的SOAP消息響應如下所示:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <ns2:createWorkItemResponse xmlns:ns2="http://someCompany.com/"> 
     <CreateWorkOrderItemResponse> 
      <Errors> 
       <errorList>Error 1</errorList> 
       <errorList>Error 2</errorList> 
      </Errors> 
      <responseCode>Testing Create 2222</responseCode> 
      <testList/> 
     </CreateWorkOrderItemResponse> 
     </ns2:createWorkItemResponse> 
    </S:Body> 
</S:Envelope> 

謝謝大家的幫助!

+0

當然,我會將「errorList」變量名改爲別的。 – Ascalonian 2012-04-10 20:25:19

+0

哦,'testList'就是我試着做的東西。這可以被忽略。 – Ascalonian 2012-04-11 14:50:10

回答

8

我想出了答案。我不得不爲此使用XmlElementWrapper註釋。所以現在的代碼是:

public class CreateWorkOrderItemResponse { 
    private String responseCode = null; 
    private ArrayList<String> errorList = new ArrayList<String>(); 

    public void setResponseCode(String responseCode) { 
     this.responseCode = responseCode; 
    } 

    public String getResponseCode() { 
     return responseCode; 
    } 

    public void addError(String error) { 
     errorList.add(error); 
    } 

    public void setErrorList(ArrayList<String> errorList) { 
     this.errorList = errorList; 
    } 

    @XmlElementWrapper(name="error_list") 
    @XmlElement(name="error") 
    public ArrayList<String> getErrorList() { 
     return errorList; 
    } 
}