JAX-WS:在返回的ArrayList周围放置一个包装器

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

最后,是响应对象的代码

And lastly, the code for the response object

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的响应如下:

When I run this code, the response back in SoapUI comes out like this:

<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消息响应如下所示:

Finally, the question... With the code above, is there a way to change it so I can add a "wrapper" around the errorList responses? I am looking to have the SOAP message response look like this:

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

谢谢大家的帮助!

我想出了答案.为此,我必须使用XmlElementWrapper批注.现在的代码是:

I figured out the answer. I had to use the XmlElementWrapper annotation for this. So the code is now:

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