2016-08-11 110 views
0

我写了一个非常简单的webservice,它返回一个ArrayList。当我尝试使用SOAPUI测试我的Web服务时,响应是空的。我在Tomcat中部署这个应用程序。返回ArrayList的SOAP响应

这里是我的代码:

@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响应。

enter image description here

+0

您是否尝试过其他任何方式,以确保您的服务能够按照您的要求工作,然后再说结果不符合预期? – Rao

+0

是的,我有。我已经实现了3个其他方法来查看服务是否有错误,其他3个返回普通字符串的方法工作正常。 – Gowtham

回答

0

的问题可能是由这个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接口

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

希望这有助于

0

我解决了,只是用下面

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

    return PersonaService.PERSONAS_REGISTRADAS; 
} 

代码只需更换列表,而不是ArrayList的。

关于;