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-issuemaster opened this issue Aug 27, 2018 · 1 comment

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Aug 27, 2018

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

@rstoyanchev

This comment has been minimized.

Copy link
Contributor

@rstoyanchev rstoyanchev commented Jan 29, 2019

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 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
Projects
None yet
2 participants
You can’t perform that action at this time.