CSSG
CSSG

Reputation: 39

How to return an array from a Restful Web service?

I have the following web service in which I want to return and values of array

@GET
@Consumes("text/plain")
@Produces("text/plain")
public String[] getText(@PathParam("name") String Uname) {
    //TODO return proper representation object
    System.out.println("In Method " + Uname);
    String arr[]=null;
     arr=new String[2];
    arr[0]="demo";
    arr[1]="demo2";
    return arr;
}

But when I test this web services it is giving me this error: GET RequestFailed RequestFailed --> Status: (406) Response: {

So what should I do if I want to return an array from a REST webservice?

Upvotes: 1

Views: 11816

Answers (1)

Alb
Alb

Reputation: 3681

HTTP responses do not support Arrays in plain text responses. You either need to manually represent your array as a String, change the return type to String and return like this:

return Arrays.toString(arr);

Or you could convert your array to a List: return Arrays.asList(arr);

and use the approach to return it as JSON or XML here: Jersey: Return a list of strings

Upvotes: 2

Related Questions