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

Registering an HttpMessageConverter while preserving default converters [SPR-12450] #17055

Closed
spring-projects-issues opened this issue Nov 18, 2014 · 7 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Nov 18, 2014

Christopher Smith opened SPR-12450 and commented

I am the maintainer for a component library that adds an embedded postback listener controller to some larger application. This controller needs a custom HttpMessageConverter due to quirky JSON supplied by the caller.

There is currently no distributed way to add HTTP converters to the Web configuration. The current best recommendation is to do something like call addDefaultHttpMessageConverters (#14257), but this component is not responsible for the overall configuration of the application.

I would like an annotation-based approach for configuring HTTP converters that could collect components from independent modules and apply them all without explicit coordination.


Affects: 4.1.2

Referenced from: commits 24834f6

@spring-projects-issues
Copy link
Collaborator Author

Christopher Smith commented

Upon reflection, what I've asked for isn't really quite what I want. I want the ability to additively register an HttpMessageConverter like I can for a Formatter or regular Converter, preferably with priority. Right now HttpMessageConverter has the annoying property of being a one-shot.

Alternatively or in addition, configuring this for a specific controller instead of globally would solve my use case. The existing controller advice (particularly @InitBinder) is similar to what I'm looking for but can't advise @RequestBody.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Multiple @EnableWebMvc configuration classes (implementing WebMvcConfigurer) can each add converters (to the same list) via configureMessageConverters and that's actually additive. The catch is that even one added converter turns off default converter registration. So I'm guessing the larger application relies on default converters (i.e. doesn't register any explicitly) in which case adding a custom converter is disruptive?

@spring-projects-issues
Copy link
Collaborator Author

Christopher Smith commented

Correct. Essentially, think of a "mixin" module like Spring Boot Actuator: It registers a controller independent of the main application. In this case, however, I need a custom converter to handle some weird JSON deserialization and don't want to otherwise modify the MVC configuration. If you just add in my mixin and it automatically registers that converter, the regular old JSON and form converters stop working, and WebMvcConfigurer can't call the method to add the default converters, which is protected for some reason.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

I completely see the motivation for the request. Having "add" and "configure" methods next to each other however is confusing considering that "configure" is also additive across multiple WebMvcConfigurer instances. The difference is more subtle and practically boils down to default converter registration.

Instead we could have extendMessageConverters(List<HttpMessageConverter<?>>) method to be invoked after the message converters list has been configured, allowing each WebMvcConfigurer to make further updates. You could then simply insert a converter.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

This is now available to try in a 4.1.3.BUILD-SNAPSHOT from http://repo.spring.io/snapshot. See commit 24834f for details.

@spring-projects-issues
Copy link
Collaborator Author

Christopher Smith commented

@Juergen, thanks for the re-title; that's exactly what I meant. @Rossen, is the List passed in guaranteed to be mutable?

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Yes, it should be. This is in WebMvcConfigurationSupport#getMessageConverters.

@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 4.1.3 milestone Jan 11, 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) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants