Skip to content
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

Provide fine-grained configuration support for HTTP message converters created in WebMvcConfigurationSupport#addDefaultHttpMessageConverters [SPR-17218] #21751

Closed
spring-projects-issues opened this issue Aug 27, 2018 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

lgoldstein opened SPR-17218 and commented

The current code creates and initializes said converters without providing the user any way to intervene and modify the defaults. E.g., provide some external ObjectMapper to the MappingJackson2HttpMessageConverter, modify the default charset of StringHttpMessageConverter etc... The only recourse currently available is to override extendMessageConverters, somehow detect the converter(s) of interest and re-initialize them - potentially repeating already existing code (a.k.a. the D.R.Y. principle). I propose the following (IMO simple) feature - add a preConfigureDefaultHttpMessageConverter method that allows users to re-configure the initial converter - e.g.:

// if returns null then discard the converter
protected <T, C extends HttpMessageConverter<T>> C preConfigureDefaultHttpMessageConverter(C converter, ApplicationContext context) {
    return converter;    // by default do nothing
}

protected <T, C extends HttpMessageConverter<T>> C addDefaultHttpMessageConverter(
        List<HttpMessageConverter<?>> messageConverters, C converter, ApplicationContext context) {
    converter = preConfigureDefaultHttpMessageConverter(converter, context);
    if (converter != null) {
        messageConverters.add(converter);
    }
    return converter;    // echo the effective converter
}

protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
    StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
    stringHttpMessageConverter.setWriteAcceptCharset(false);  // see SPR-7316
    addDefaultHttpMessageConverter(messageConverters, new ByteArrayHttpMessageConverter(), applicationContext);
    addDefaultHttpMessageConverter(messageConverters, stringHttpMessageConverter, applicationContext);
    ...etc...
}

Affects: 5.0.8

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.x Backlog milestone Jan 11, 2019
@rstoyanchev
Copy link
Contributor

Fine idea but it's yet another option on top of multiple existing options, not to mention the
extensive further options in Spring Boot. Using Java 8 Stream API with a filter on the converter type should provide a pretty good compromise.

@rstoyanchev rstoyanchev added the status: declined A suggestion or change that we don't feel we should currently apply label Jan 29, 2019
@rstoyanchev rstoyanchev self-assigned this Jan 29, 2019
@rstoyanchev rstoyanchev removed this from the 5.x Backlog milestone Jan 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants