How can return List of Strings in REST Web Services
I am using CXF 2.7.8
I have one method as :
@GET@Path("/items")@Produces(MediaType.APPLICATION_JSON)public List<String> getItems() { List<String> list = service.getList(); return list;}
I am getting error as "No message body writer has been found for response class ArrayList."
Option 1
I have also tried to use GenericEntity> and got same error as above
Option 2
I have also created Wrapper class for list as
@XmlRootElement(name = "listWarpper")public class ListWarpper implements Serializable {private static final long serialVersionUID = 1L;private List<String> list;public ListWarpper() {}public ListWarpper( List<String> list ) { this.list = list;}public List<String> getList() { return list;}public void setList( List<String> list ) { this.list = list;}
}
and it works fine but only problem with it is when result have just one item into list then returned json is
{ list: "Only one Item"}
instead of `{list : ["Only one Item"]}
My problem is how can I get returning json in form of as follows no matter if list is empty or containing just one element
{ list : [...]}
or {[...]}