Skip to content

Adding a HttpMessageConverter to the end was possible with deprecated extendMessageConverters(…) but not with its replacement configureMessageConverters(…) #36043

@AlexanderWirthSAP

Description

@AlexanderWirthSAP

With WebMvcConfigurer.extendMessageConverters(List<HttpMessageConverter<?>> converters) it was possible to add a custom HttpMessageConverter to the front, back or anywhere in the middle. This method has been deprecated and the documented replacement is WebMvcConfigurer.configureMessageConverters(HttpMessageConverters.ServerBuilder builder).

However, in configureMessageConverters the builder only offers one method addCustomConverter which always adds the custom converter to the front of the list. In my application I have a custom HttpMessageConverter that supports MediaType.ALL and canWrite(Class<?> clazz, @Nullable MediaType mediaType) returns true if the clazz is an instance of my custom error object. This converter is supposed to be a fallback in case the other converters failed to handle the response which can happen if the client sends an accept-header that only accepts unusual media types.

This is only supposed to be a fallback and therefore should be the last converter in the list. Otherwise it will handle the error objects before, for example, JacksonJsonHttpMessageConverter gets a chance to handle it.

How do I achieve this in a non-deprecated way?
There was already a feature request for this in issue #35177 which was closed after implementing the current behavior. However, the current behavior does not actually allow ordering the message converters. Can this be added as an enhancement to the ServerBuilder?

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: waiting-for-triageAn issue we've not yet triaged or decided on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions