1

Suppose I have two endpoints that look like this:

@GET
@Path("/blah")
@Produces(MIME_TYPE_1)
public Thing getThing() {
    ....
}

@GET
@Path("/blah")
@Produces(MIME_TYPE_2)
public OtherThing getOtherThing() {
    ....
}

This works very well for arbitrating which method gets invoked based on the Accept header that the client sends.

The problem I'm having is that if the client misses off the Accept header entirely, I (for some reason) get the second method invoked, and I want it to be the first one.

There's an additional complication, which is that this is feeding automatically into Swagger documentation, and I don't want extra MIME types to appear in the documentation, so I don't want (for instance) to add */* to the types accepted by the first method.

(This is using Dropwizard and Jersey 1.x, though I am still interested to hear solutions based on Jersey 2.x, to which we may upgrade before long.)

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • You _might_ be able to filter the request and modify the `Accept` header before Jersey queries it. I don't think it would work with a `ContainerRequestFilter` but it might with a `ServletFilter`. – condit Apr 24 '15 at 17:53

1 Answers1

2

Try using the quality factor parameter in the @Produces annotation:

@GET
@Path("/blah")
@Produces("application/json; q=0.6")
public Thing getThing() {
....
}

@GET
@Path("/blah")
@Produces("application/xml; q=1")
public OtherThing getOtherThing() {
....
}

If a client does not send the 'Accept:' header in the request, the web server will execute getOtherThing() because it has a higher quality factor than getThing().

Disclaimer: this works with RestEasy, but I cannot guarantee it will work with Jersey.

The quality factor parameter is part of RFC 2616 (14.1)
You can find more info about this feature in RestEasy here

David C
  • 58
  • 5