Json serialization fails with strange error

23 views
Skip to first unread message

Mike Thomsen

unread,
Feb 9, 2016, 9:23:33 PM2/9/16
to dropwizard-user
Even a message like this fails:

{
    "message": "Hi!"
}

with this server-side error:

! java.lang.IllegalStateException: Committed
! at org.eclipse.jetty.server.Response.resetBuffer(Response.java:1243) ~[test-jar.jar:na]
! at org.eclipse.jetty.server.Response.sendError(Response.java:567) ~[test-jar.jar:na]
! at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:484) ~[test-jar.jar:na]
! at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425) ~[test-jar.jar:na]
! at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383) ~[test-jar.jar:na]
! at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336) ~[test-jar.jar:na]
! at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223) ~[test-jar.jar:na]
! at io.dropwizard.jetty.NonblockingServletHolder.handle(NonblockingServletHolder.java:49) ~[test-jar.jar:na]
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1669) ~[test-jar.jar:na]
! at io.dropwizard.servlets.ThreadNameFilter.doFilter(ThreadNameFilter.java:29) ~[test-jar.jar:na]
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) ~[test-jar.jar:na]
! at io.dropwizard.jersey.filter.AllowedMethodsFilter.handle(AllowedMethodsFilter.java:43) ~[test-jar.jar:na]
! at io.dropwizard.jersey.filter.AllowedMethodsFilter.doFilter(AllowedMethodsFilter.java:38) ~[test-jar.jar:na]
! at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) ~[test-jar.jar:na]
! at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) [test-jar.jar:na]
! at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) [test-jar.jar:na]
! at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) [test-jar.jar:na]
! at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) [test-jar.jar:na]
! at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [test-jar.jar:na]
! at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [test-jar.jar:na]

This is the method that's failing:

    @GET
    @Path("/test/{id}")
    public Response test(@PathParam("id")
                                      String id, @QueryParam("alt")
                                      String alternativeSerializationFormat) {
        Response getEntityResponse;

        String message  = "{\"message:\": \"Hi!!!\"}";
        getEntityResponse = Response.ok().entity(message).type(MediaType.APPLICATION_JSON_TYPE)
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_TYPE+"; charset=UTF-8").build();
        return getEntityResponse;
    }

I'm thinking that it might be a classpath issue. Does anyone have a recommendation on what could be causing this? We use Avro which forces us to have Jackson 1 on our classpath as well as Jackson 2.

Thanks,

Mike

Graham O'Regan

unread,
Feb 10, 2016, 5:03:52 AM2/10/16
to dropwiz...@googlegroups.com
You are setting the content type twice using type() and then setting the header. You should let the framework handle the content negotiation for you, try this;

  @GET
  @Path(“/test/{id}“)
  @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
  public Response test(@PathParam("id")
                       String id, @QueryParam("alt")
                       String alternativeSerializationFormat) {
    Response getEntityResponse;

    String message  = "{\"message:\": \"Hi!!!\"}";
    getEntityResponse = Response.ok()
                                .entity(message)
                                .build();
    return getEntityResponse;
  }

Using @Consumes and @Produces should let you handle the content types

--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Thomsen

unread,
Feb 10, 2016, 7:47:39 AM2/10/16
to dropwizard-user
Thanks. I actually figured that out by trial and error. It wasn't originally my code, but oddly enough it worked just fine with Jersey 1 and Jackson 1. It wasn't until we attempted a migration from a traditional JavaEE deployment to DropWizard that it even came up!
Reply all
Reply to author
Forward
0 new messages