Search code examples
javaspringspring-mvcjerseycontent-negotiation

Can you specify preferred default media type for a single path in Spring MVC?


I have a Jersey application which has been converted to Spring MVC. One piece of functionality that I don't see a way to port directly over is the ability, per path, to specify the preferred media type if none is specified. In Jersey, I could specify the "qs" property on the media type, and it would use that to determine which response type to send if none were specified (or if multiple options were specified in the Accept header, I believe this value was multiplied by the quality scores specified).

@Produces("application/json")
@GET
@Path("/some/path")
public Response preferredResponseType() {
  //Implementation goes here
}

@Produces({"application/schema+json;qs=0.9"})
@GET
@Path("/some/path")
public Response otherResponseType() {
  //Implementation goes here
}

In this example, if I do a GET request against /some/path with no Accept header, it will return the application/json response.

I don't see any easy way to do this in Spring MVC, particularly not if I want to restrict the default to applying to just that one endpoint (there are other endpoints in the app that should have a different preferred default). I do see that there is a way to globally set a default content type (per the "defaultContentType" and "defaultContentTypeStrategy" methods in ContentNegotiationConfigurer), but that does not easily address the per-path use case.

Is there an easy way to achieve per-path media type defaults different from the application global default?


Solution

  • Spring issue 19050 requests this functionality. Per the conversation there, it looks like there is no simple way to declaratively specify the default content type to use. Furthermore, the Spring team has closed the issue with a decision not to implement this functionality.