Java JAX-WS:在返回的ArrayList周围放置一个包装器
我有以下端点接口:Java JAX-WS:在返回的ArrayList周围放置一个包装器,java,web-services,jax-ws,Java,Web Services,Jax Ws,我有以下端点接口: @WebService public interface SEIWebService { @WebMethod @WebResult(name="CreateWorkOrderItemResponse") CreateWorkOrderItemResponse createWorkItem(@WebParam(name = "CreateWorkOrderItemRequest")CreateWorkOrderItemRequest request)
@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;
}
}
公共类CreateWorkOrderItemResponse{
私有字符串responseCode=null;
private ArrayList errorList=新建ArrayList();
公共void setResponseCode(字符串responseCode){
this.responseCode=responseCode;
}
公共字符串getResponseCode(){
返回响应代码;
}
公共无效加法器(字符串错误){
errorList.add(错误);
}
公共无效setErrorList(ArrayList errorList){
this.errorList=errorList;
}
公共ArrayList getErrorList(){
返回错误列表;
}
}
当我运行此代码时,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>
<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>
错误1
错误2
测试创建2222
最后,问题。。。对于上面的代码,是否有办法更改它,以便在errorList响应周围添加“包装器”?我希望SOAP消息响应如下所示:
<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>
<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>
错误1
错误2
测试创建2222
谢谢大家的帮助 我找到了答案。为此,我不得不使用
xmlementwrapper
注释。因此,代码现在是:
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;
}
}
公共类CreateWorkOrderItemResponse{
私有字符串responseCode=null;
private ArrayList errorList=新建ArrayList();
公共void setResponseCode(字符串responseCode){
this.responseCode=responseCode;
}
公共字符串getResponseCode(){
返回响应代码;
}
公共无效加法器(字符串错误){
errorList.add(错误);
}
公共无效setErrorList(ArrayList errorList){
this.errorList=errorList;
}
@XmlElementWrapper(name=“error\u list”)
@xmlement(name=“error”)
公共ArrayList getErrorList(){
返回错误列表;
}
}
我当然会将“errorList”变量名更改为其他名称。噢,testList
只是我在尝试一些东西。这是可以忽略的。