简体   繁体   English

SOAP响应返回ArrayList

[英]SOAP response returning ArrayList

I wrote a very simple webservice which returns a ArrayList. 我写了一个非常简单的webservice,它返回一个ArrayList。 when I try to test my web service using SOAPUI , the response is empty. 当我尝试使用SOAPUI测试我的Web服务时,响应为空。 I am deploying this application in Tomcat. 我正在Tomcat中部署此应用程序。

Here is my code: 这是我的代码:

@WebService(endpointInterface = "com.enterprise.ws.WebServiceInterface")
public class WebServiceImpl implements WebServiceInterface{

    @Override
    public ArrayList<String> listSample() {
        // TODO Auto-generated method stub
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("1212");
        return arrayList;
    }
}

Interface : 界面:

@WebService
@SOAPBinding(style = Style.RPC)
public interface WebServiceInterface {

    @WebMethod
    ArrayList<String> listSample();

}

Here is my SOAPUI response . 这是我的SOAPUI响应。

在此输入图像描述

The problem is probably caused by this JAX-B bug: https://java.net/jira/browse/JAXB-223 问题可能是由于这个JAX-B错误引起的: https//java.net/jira/browse/JAXB-223

The problem is that if you use a JAX-WS 2.0/JAX-B 2.0 you can not use a collection class directly as a return type for a @WebMethod . 问题是,如果使用JAX-WS 2.0 / JAX-B 2.0 ,则不能直接将集合类用作@WebMethod的返回类型。

There are two possibles workarounds to avoid this issue: 有两种可能的解决方法可以避免此问题:

One is to use an Array instead of ArrayList avoiding the use of collection class: 一种是使用Array而不是ArrayList避免使用集合类:

Interface 接口

@WebService
@SOAPBinding(style = Style.RPC)
public interface WebServiceInterface {

    @WebMethod
    String[] listSample();
}

Implementation 履行

@WebService(endpointInterface = "com.enterprise.ws.WebServiceInterface")
public class WebServiceImpl implements WebServiceInterface{

    @Override
    public String[] listSample() {
        return new String[]{"1212"};
    }
}

Another possible workaround is to create a POJO to wrap your ArrayList , and on the @WebMethod return the POJO type instead: 另一种可能的解决方法是创建一个POJO来包装您的ArrayList ,并在@WebMethod返回POJO类型:

POJO class POJO课程

public class PojoSample {

     private List<String> listSample;
     // create getters and setters
     ...
}

POJO Interface POJO接口

@WebService
@SOAPBinding(style = Style.RPC)
public interface WebServiceInterface {

    @WebMethod
    PojoSample listSample();
}

POJO Implementation POJO实施

@WebService(endpointInterface = "com.enterprise.ws.WebServiceInterface")
public class WebServiceImpl implements WebServiceInterface{

    @Override
    public PojoSample listSample() {
        List<String> arrayList = new ArrayList<String>();
        arrayList.add("1212");

        PojoSample pojo = new PojoSample();
        pojo.setListSample(arrayList);
        return pojo;
    }
}

Hope this helps, 希望这可以帮助,

I Solved that just using the code below 我解决了只使用下面的代码

 @WebMethod(operationName = "listarPersonas")
public List<Persona> listarPersonas() {

    return PersonaService.PERSONAS_REGISTRADAS;
}

Just replace List instead Arraylist. 只需替换List而不是Arraylist。

regards; 问候;

Try soap binding document with wrapped parameter style insted of RPC binding. 尝试使用RPC绑定的包装参数样式的soap绑定文档。

@SOAPBinding(style=Style.DOCUMENT,use=Use.LITERAL, parameterStyle=ParameterStyle.WRAPPED)

With this you can write web method with List return value, or parameter as a List: 有了这个,您可以使用List返回值或参数作为List编写Web方法:

@WebMethod
@WebResult(name = "returnName")
public List<MyBean> methodName(@WebParam(name = "paramName") List<ParamBean> paramList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM