-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configure the ObjectMappers to Use for a Class by MediaType #26212
Comments
Thanks for the sample. I see what you're pointing out with the choice of In terms of being supported, when Is there any support for @GetMapping(path = "/", produces = {MediaTypes.HAL_JSON_VALUE, ...})
RepresentationModel<?> index() {
return new RepresentationModel<>().add(Link.of("/url"));
} |
After a direct discussion, a summary of the problem space.
When a client requests both One option would be to configure A second line of thought is to allow registrations of additional |
I think that registering additional, While ordering of converters and its effect is by design, this additional level would make the current use case more obvious in our infrastructure: we need to have specialized conversions depending on the media type, even if those are all JSON-compatible. I'm not sure that the first solution (i.e. "opting out based on the object type") would work in other cases. In general, I'm wondering if we shouldn't change the defaults for the JSON converter and support |
Team decision: we'll proceed with a prototyping support for more than one ObjectMapper to be used. |
Take the following
HttpMessageConverter
setup in a Spring Boot application:HMC
registered to handle theapplication/hal+json
media type (via Spring HATEOAS).MappingJackson2HttpMessageConverter
registered to handleapplication/json
andapplication/*+json
.An HTTP request with
Accept: application/prs.hal-forms+json, application/hal+json
header will produce a response withContent-Type: application/prs.hal-forms+json
which I think is wrong. The returned content type should beapplication/hal+json
.Here's what happens:
AbstractMessageConverterMethodProcessor.writeWithMessageConverters(…)
iterates over all acceptable types, starting withapplication/prs.hal-forms+json
and checks it for compatibility with the producible media types. The HALHMC
does not match but the default Jackson one matches due to the support ofapplication/*+json
.In the next step,
getMostSpecificMediaType(…)
is called and that match turns from a wildcard match into a concrete match. I.e. the match is treated as ifapplication/prs.hal-forms+json
had been matched explicitly. The now following concrete match for the HALHMC
andapplication/hal+json
ends up second in the line inmediaTypesToUse
.That "upgrade" in the match causes the subsequent
MediaType.sortBySpecificityAndQuality(mediaTypesToUse)
to be without effect on the ordering as the first, actually wildcard match, is treated as concrete. As a consequence, the fallbackHMC
gets used instead of the actual concrete match ofapplication/hal+json
and is not even able to render the object according to the media type specification.I've prepared a reproducing example here.
The text was updated successfully, but these errors were encountered: