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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10095911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:36:11  来源:igfitidea点击:

JAX-WS: Place a wrapper around an ArrayList being returned

javaweb-servicesjax-ws

提问by Ascalonian

I have the following endpoint interface:

我有以下端点接口:

@WebService
public interface SEIWebService {

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

The implementation:

实施:

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

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

当我运行此代码时,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>

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:

最后,问题......使用上面的代码,有没有办法改变它,这样我就可以在 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>

Thank you all for the help!

谢谢大家的帮助!

回答by Ascalonian

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

我想出了答案。我不得不为此使用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;
    }
}