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?

3
  • 3
    Why do you use so many question marks?????????????? Commented Mar 2, 2013 at 1:24
  • Do you want to return an array represented as XML? This might help: java.dzone.com/articles/xml-bindings-jaxb-and-jax-rs Commented Mar 2, 2013 at 1:36
  • Not really I want to return as Plain Text I have made the change in the original post. Commented Mar 2, 2013 at 1:46

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.