gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:43:07 26 4
gpt4 key购买 nike

我有以下端点接口(interface):

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

谢谢大家的帮助!

最佳答案

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

关于java - JAX-WS:在返回的 ArrayList 周围放置一个包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10095911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com