1

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?

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
CSSG
  • 39
  • 1
  • 1
  • 5

1 Answers1

2

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

Community
  • 1
  • 1
Alb
  • 3,601
  • 6
  • 33
  • 38
  • Hey, your first trick works. Could be please explain this statement with some example " encode your response as as JSON array and return that as a String " How to do this I have no idea ? – CSSG Mar 02 '13 at 01:56
  • @CSSG I edited my answer to link to another post with more details. This is only useful however if you want your response as JSON or XML instead of plain text. – Alb Mar 02 '13 at 01:58