users@jersey.java.net

[Jersey] Error returning string list in Web service method

From: Peter Tap <ptrtap_at_yahoo.com>
Date: Thu, 28 Jul 2011 08:41:08 -0700 (PDT)

Pavel,

Thank you for your help.

Changing the return type from String[] to List<String> does not get rid of the error. The error message is slightly different:

SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException:
A message body writer for Java class java.util.ArrayList, and Java type java.util.List<java.lang.String>, and MIME media type application/json was not found

There must be something that I am missing fundamentally. I also ensured that dependency "jersey-json" is present in pom.xml: Is there anything else that I have overlooked?

Appreciate your help.

Regards,
Peter

-----------------

JAX-RS don't support String[] afaik, you would need to write your own MessageBodyReader/Writer. But there is much easier solution: return List<String> instead.

Regards,
Pavel







________________________________
From: Peter Tap <ptrtap_at_yahoo.com>
To: "users_at_jersey.java.net" <users_at_jersey.java.net>
Sent: Wednesday, July 27, 2011 5:54 PM
Subject: Re: Missing maven dependency


Folks,

I generated a sample "Jersey + Grizzly2" app from the Maven archetype catalog at http://download.java.net/maven/2. It creates a simple class that produces "text/plain" output: I changed it a little to produce json output as shown below:


@Path("/myresource")
public class MyResource {
    @GET
    @Produces("application/json")
    public String getIt() {
        return "Got it!";
    }
}


I also ensured that dependency "jersey-json" is uncommented in pom.xml:

- <dependency>
 <     <groupId>com.sun.jersey</groupId>
 <    <artifactId>jersey-json</artifactId>
   <version>${jersey-version}</version>
</dependency>

When I run the application and browse from my browser, I see that it generates the string output as expected.

Now, I made a simple change. Instead of returning a single string, I am returning an array of strings:


    public String[] getIt() {
        String[] retVal = {"a", "b"};
        return retVal;   
    }


However, when I run the application with this change, I get the following exception:


SEVERE: Mapped exception to response: 500 (Internal Server Error)

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException:
A message body writer for Java class [Ljava.lang.String;, and Java type class [Ljava.lang.String;, and MIME media type application/json was not found.
How do I fix this problem?

Thank you in advance for your help.

Regards,
Peter