user1801279
user1801279

Reputation: 1783

jersey working for media type text but not json or xml

I have the following dependencies for jersey in my web application

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.8</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.8</version>
    </dependency>

when I use @Consumes(MediaType.APPLICATION_JSON) or @Produces(MediaType.APPLICATION_JSON) i get a server error . Similarly if I try and use XML . Is it because of the libraries I am using ?

my java class

import org.apache.log4j.Logger;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


@Path("/test")
public class test {

    private static Logger logger = Logger.getLogger(test.class);



    @GET
    @Produces(MediaType.TEXT_PLAIN)  // works perfectly
    public Response getMsg()
    {
    logger.info("Inside getMsg()");

    String output = "hello world";

    return Response.status(200).entity(output).build();

    }

  /*   @GET                                      // I get a HTTP 500 , server error 
     @Path("/get")
     @Produces(MediaType.APPLICATION_JSON)
     public Track getTrackInJSON() {

         Track track = new Track();
         track.setTitle("Enter Sandman");
         track.setSinger("Metallica");

         return track;
     }
    */


    @GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getTrackInJSON() {

        Track track = new Track();
        track.setTitle("Enter Sandman");
        track.setSinger("Metallica");

        //String test = " hello world";
        return Response.status(201).entity(track).build();

    }


    @POST
    @Path("/post")
    @Consumes(MediaType.APPLICATION_JSON)
 //   @Produces(Me)

    public Response createTrackInJSON(Track track) {
        String result = "Track saved : " + track;
        return Response.status(201).entity(result).build();

    }

    @POST
    @Path("/getValue")
    @Produces(MediaType.APPLICATION_XML)
    public void createXML()
    {
        String result = " get the track";
        //        return result.createXML();
        // check this line

    }



        }

when I use a rest client to access the @GET resource I get this error :

<u>Internal Server Error</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u>

when I use the rest client to access the @POST resource I get this error:

<u>Internal Server Error</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u>

this is what I get from the Tomcat error logs

Jan 29, 2014 11:00:21 AM com.sun.jersey.spi.container.ContainerResponse logException
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 com.myProj.jsp.rest.Track, and Java type class com.gsipartners.apimgmtut
        at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
        at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
        at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
        at java.lang.Thread.run(Thread.java:662)
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class com.myProj.jsp.rest.Track, and Java type class com.myProj.jsp.rest.Track, and MIM
        ... 24 more

the Track class

public class Track {

    String title;
    String singer;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSinger() {
        return singer;
    }

    public void setSinger(String singer) {
        this.singer = singer;
    }

    @Override
    public String toString() {
        return "Track [title=" + title + ", singer=" + singer + "]";
    }

}

Upvotes: 0

Views: 3468

Answers (1)

Xavier Coulon
Xavier Coulon

Reputation: 1600

You should annotate your Track class with JAXB annotations and Jersey will use its MessageBodyWriter to serialize/marshall the returned entity (track) into the response outputstream. Or you'll need to provide your own implementation of MessageBodyWriter for the Track type to write the serialized form in the response outputstream. Otherwise, Jersey doesn't know how to do it.

Upvotes: 1

Related Questions