2

我有以下端点接口:

@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>

谢谢大家的帮助!

4

1 回答 1

10

我想出了答案。我不得不为此使用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;
    }
}
于 2012-04-11T15:54:21.100 回答