Jersey (dropwizard) default for type @Produces and @Consumes

By default, all methods in our application will be JSON based. I would like not to add @Produces and @Consumes (possibly optional, optional) for each resource class as shown below.

@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class TestResource {

  @GET
  public TestClass test() {
    return new TestClass();
  }
}

      

It currently uses application / octet-stream if I don't define the media type. I was hoping there was a way to change it to something else via jersey ResourceConfig or something. Looked at the source code but got lost.

Dropwizard is currently using jersey 1.18 but will be updated to 2.0 soon (hopefully :)). So answers to both versions would be appreciated.

+3


source to share


1 answer


Perhaps this can be done through the ContainerRequestFilter. There you can change the ACCEPT header. If you only set json for this it might help. Have you tried setting the ACCEPT header field in your REST client for JSON only? Does it help?

Or, you simply construct your response with json like this:



@GET
@Path("get")
public Response getInTestResource()
{
    return Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity("get is ok").build();
}

      

But one question: does it really work if you document what the method consumes or produces? I think the annotation doesn't make the extra work too easy ...

+1


source







All Articles