1

I am trying to make a generic REST api using Jersey and I followed the following blog for this: https://theza.ch/2009/08/11/uri-extensions-in-jersey/

So what is happening is that the server is working fine when I use .xml in my url and when I use .json, it gives a 500 Internal Server error. I have tried different things,but to no avail. Could anyone by any change know why this is happening in json and not for xml and how to fix this?

My code looks something like this:

@GET
@Path("/order/{product-key}/getorderid")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getOrderIdByDomain(@Context HttpServletRequest request,
                                   @PathParam("product-key") final String productKey,
                                   @QueryParam("domain-name") final String domainName ) throws Exception
{
    try
    {
        Integer response = doSomething();

        return Response.status(200).entity(response).build();
    }
    catch (Exception lbe)
    {
        Hashtable response = new Hashtable();
        response.put("Error",lbe.getMessage());
        return Response.status(400).entity(response).build();
    }
}

UPDATE:

After adding the jersey-json dependency, the 500 error changed to 200 OK but I am still getting an empty response. For xml, I am getting the expected response. Did someone face a similar issue? Please suggest something because I have tried a few things from other answers but it doesn't seem to be working.

StackTrace:

Caused by: java.lang.AbstractMethodError
at org.codehaus.jackson.map.AnnotationIntrospector$Pair.findSerializer(AnnotationIntrospector.java:1148)
at org.codehaus.jackson.map.ser.BasicSerializerFactory.findSerializerFromAnnotation(BasicSerializerFactory.java:362)
at org.codehaus.jackson.map.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:252)
at org.codehaus.jackson.map.ser.StdSerializerProvider._createUntypedSerializer(StdSerializerProvider.java:782)
at org.codehaus.jackson.map.ser.StdSerializerProvider._createAndCacheUntypedSerializer(StdSerializerProvider.java:735)
at org.codehaus.jackson.map.ser.StdSerializerProvider.findValueSerializer(StdSerializerProvider.java:344)
at org.codehaus.jackson.map.ser.StdSerializerProvider.findTypedValueSerializer(StdSerializerProvider.java:420)
at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:601)
at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1606)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:520)
at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160)
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
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:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
... 48 more

I am using the following dependencies: jersey-server, jersey-json version 1.8.

doctore
  • 518
  • 9
  • 18
  • I think you are missing some dependency of jackson , refer this and include it in your project http://stackoverflow.com/questions/18429468/correct-set-of-dependencies-for-using-jackson-mapper – Jekin Kalariya Sep 06 '16 at 12:39
  • Could you share with us exception stack trace? – Robertiano Sep 06 '16 at 13:00
  • 1
    That tutorial is almost 7 years old now and it's based on the *old* Jersey 1.x. Are you doing something *new*? Have you ever considered using Jersey 2.x? – cassiomolin Sep 06 '16 at 13:42
  • @JekinKalariya Adding a jackson dependency did help. I am getting a 200 now but an empty response. whereas for xml, i am getting the expected response. – doctore Sep 06 '16 at 14:34
  • @JekinKalariya anything else you think i might have missed? – doctore Sep 07 '16 at 09:31
  • how will you decide weather to genrate xml or json is there any key? – Jekin Kalariya Sep 07 '16 at 09:51

2 Answers2

2

After trying for long to fix this, i switched to genson,

        <dependency>
            <groupId>com.owlike</groupId>
            <artifactId>genson</artifactId>
            <version>1.3</version>
        </dependency>

It worked very easily. Will try to find later why Jackson didn't work.

doctore
  • 518
  • 9
  • 18
0

After adding jar dependency as suggested in comment,
you need have additional parameter to decide xml or json named 'format' ,so now you can do like this by changing return statement as below

@GET
    @Path("/order/{product-key}/{format}/getorderid")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response getOrderIdByDomain(@Context HttpServletRequest request,
                                       @PathParam("product-key") final String productKey,@PathParam("format") final String format,
                                       @QueryParam("domain-name") final String domainName ) throws Exception
    {
        try
        {
            Integer response = doSomething();

           return Response
                // Set the status and Put your entity here.
                .ok(entity)
                // Add the Content-Type header to tell Jersey which format it should marshall the entity into.
                .header(HttpHeaders.CONTENT_TYPE, "json".equals(format) ? MediaType.APPLICATION_JSON : MediaType.APPLICATION_XML)
                .build();
        }
        catch (Exception lbe)
        {
            Hashtable response = new Hashtable();
            response.put("Error",lbe.getMessage());
            return Response.status(400).entity(response).build();
        }
    }
Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32
  • I am aware of this method to achieve that, but i didn't want to send an extra PathParam and the link which I posted in my question claims to be doing just that. I am trying to figure out what am I doing wrong. – doctore Sep 07 '16 at 10:19
  • can you jus tell me which jars you inserted and also put your request call from client – Jekin Kalariya Sep 07 '16 at 10:52
  • @Jekin, he can specify what type of response he expects in his client (E.G. using request.accept(MediaType.APPLICATION_JSON) – dsp_user Sep 07 '16 at 13:27
  • The server (Jersey) will have to look up the request header and see what type is specified in the accept attribute and then create either JSON or XML. – dsp_user Sep 07 '16 at 13:40