我编写了一个非常简单的 Web 服务,它返回一个 ArrayList。当我尝试使用 SOAPUI 测试我的 Web 服务时,响应为空。我正在 Tomcat 中部署此应用程序。
这是我的代码:
@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;
}
}
界面:
@WebService
@SOAPBinding(style = Style.RPC)
public interface WebServiceInterface {
@WebMethod
ArrayList<String> listSample();
}
这是我的 SOAPUI 响应。
最佳答案
该问题可能是由这个 JAX-B 错误引起的:https://java.net/jira/browse/JAXB-223
问题是,如果您使用JAX-WS 2.0/JAX-B 2.0,则不能直接使用集合类作为@WebMethod
的返回类型.
有两种可能的解决方法可以避免此问题:
一种是使用 Array
而不是 ArrayList
避免使用集合类:
界面
@WebService
@SOAPBinding(style = Style.RPC)
public interface WebServiceInterface {
@WebMethod
String[] listSample();
}
实现
@WebService(endpointInterface = "com.enterprise.ws.WebServiceInterface")
public class WebServiceImpl implements WebServiceInterface{
@Override
public String[] listSample() {
return new String[]{"1212"};
}
}
另一种可能的解决方法是创建一个POJO来包装您的ArrayList
,并在@WebMethod
上返回POJO 输入:
POJO类
public class PojoSample {
private List<String> listSample;
// create getters and setters
...
}
POJO接口(interface)
@WebService
@SOAPBinding(style = Style.RPC)
public interface WebServiceInterface {
@WebMethod
PojoSample listSample();
}
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;
}
}
希望这有帮助,
关于java - 返回 ArrayList 的 SOAP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38907593/