users@jersey.java.net

[Jersey] Re: Missing maven dependency

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Thu, 28 Jul 2011 03:13:38 +0200

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

On 7/28/11 2:54 AM, Peter Tap wrote:
> 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