-
Notifications
You must be signed in to change notification settings - Fork 41.1k
ConversionService is inconsistently used in Spring Boot application #6222
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
Comments
The Do you have an example project that you can share to show the problem? Conversion services are used in multiple places, so having some code to look at help a lot. |
It's worth noting that we are explicitly trying to not use classspath scanning -- does your "automatically detected and supported" comment require that as a prerequisite? I've had so much trouble over the years with classpath scanning that we are trying to explicitly configure everything up front. I will try to produce a self contained example next week, I ran out of time to do it this week... |
@stevenschlansker No, it's not related to classpath scanning. See these lines or |
Thanks for the reference -- that seems to cover a very small subset of what https://github.com/spring-projects/spring-framework/blob/v4.3.0.RELEASE/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java#L157-L194 provides. It only has Here is a small test case showing how confusing this situation is: https://gist.github.com/stevenschlansker/c432a6360fe8897a9cd9af6cd16e4719 You'll note that registering a custom |
Oh wow, my bad. You're totally right. By default the @jhoeller Is there any reason why |
That's a great improvement, although still wouldn't cover the case of trying to add your own non-default converters, only built-ins. |
Thinking on this a bit more, it's not going to be possible to create the static class Initializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
// customize conversionService
applicationContext.getEnvironment().setConversionService(conversionService);
}
} |
If that is the case, then I believe section 9.5.5 "Configuring a ConversionService" of the documentation is extremely confusing.
This strongly implies that a conversion service registered as a bean will be used "whenever a type conversion needs to be performed by the framework", which clearly is not happening. |
Indeed, that is quite confusing. It seems that the |
Indeed, I wonder whether we should simply also apply it to the |
@jhoeller Perhaps with |
We also need to be careful not to replace the |
|
The replacement part is where it gets tricky. We fundamentally don't know whether the |
As for further JSR-310 support at the |
Why is |
|
I'm running into a similar issue with I tracked this down to the fact that even though It would be nice if the usage of |
I am also running into a similar issue with a custom Through debugging, I've noticed that while my application is starting up, my converter is available as I would expect. However, once a request goes through to my Spring MVC controller, suddenly my converter is not there, which results in the following exception:
This is the line I have my breakpoint on: https://github.com/spring-projects/spring-framework/blob/v4.1.6.RELEASE/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java#L170 Note that there is a I've tried various things, including providing my Spring Application with a custom initializer (as suggested above) to no avail. I cannot seem to get access to my converter there. I must say, at first glance using |
@kflorence You converters might be registered with the wrong conversion service. Try putting a If you want to register converters with that service you can add a @Component
public class MyWebMvcConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(...);
}
} |
@philwebb Thanks for the quick reply -- aha! So that's where that conversion service is coming from. Out of curiosity, why does Spring MVC always create it's own conversion service instead of using an existing one (if one exists)? Also, it would be nice if there was some way of telling Spring Boot to automatically add my converter to this conversion service instead of the other one it automatically adds it to (the global default one?). Anyways, I think this configuration will work for me, but my |
Seems like there is some discussion of accessing the |
This seems to work: @Component
class SpringContextListener extends ApplicationListener[ContextRefreshedEvent] with WrapAsScala {
@Inject private[converter] var conversionService: GenericConversionService = _
@Inject private[converter] var genericConverters: java.util.Set[GenericConverter] = _
def onApplicationEvent(event: ContextRefreshedEvent): Unit = {
genericConverters.foreach(conversionService.addConverter)
}
} Hallelujah! 😃 |
What we tried to do:
|
@jhoeller and @philwebb : I am solving similar issue. Spring Boot app with Java8 and JSR-303/JSR-349 Bean Validation. Input DTO object that represents request has OffsetDateTime field. This filed has to be annotated with I want to configure Spring Boot application globally and avoid to use |
Since Spring Boot 2.0 we now have an |
I apologize in advance for cross-posting, but I have not received useful help on Stack Overflow and I suspect that the issue I am facing is either a bug in Spring Boot or at least very poor documentation. Original context here:
http://stackoverflow.com/questions/37952166/spring-boot-test-case-doesnt-use-custom-conversion-service
Short version is, I would like to configure my ConversionService to understand new types (i.e.
java.time.Duration
). Per the docs, I try to wire it up with:but it keeps being ineffective:
The BeanFactory still has a
DefaultConversionService
sticking around from before I set my own.I can fix this one with one hack:
and then I get a different problem, where again the
Environment
doesn't have it set either!Time for another hack:
And that fixes this problem.
But how long until I find the next place one of these icky
DefaultConversionService
instances is lying around? How can I make Spring Boot actually use my custom one for everything without having to keep diving deep into the guts and find lingering problems?Spring 4.3.0, Spring Boot 1.4.0M3
The text was updated successfully, but these errors were encountered: