Skip to content

Converters don't get picked up for MVC controllers in 3.0.7 [SPR-9157] #13795

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

Closed
spring-projects-issues opened this issue Feb 23, 2012 · 8 comments
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

Comments

@spring-projects-issues
Copy link
Collaborator

Ferenc Gratzer opened SPR-9157 and commented

The same converter class and xml config work with Spring 3.0.5 and 3.0.6, but not with 3.0.7. Only changing the Spring version breaks it.

public class EnvironmentConverter implements Converter<String, Environment> {
  public Environment convert(String source) {}
}

servlet.xml:

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
 <property name="converters">
   <set>
     <bean class="....controller.util.StringToEntityConverterFactory" />
     <bean class="....web.util.EnvironmentConverter" />
   </set>
 </property>
</bean>

Log:

org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type '....environment.Environment'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [....environment.Environment]: no matching editors or conversion strategy found
	at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:58)
	at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:553)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:507)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:340)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

Affects: 3.0.7

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

Added code formatting tags.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

It would help speed things up if you could help demonstrate the issue at the spring-framework-issues repository. There are template projects to help you do that quickly.

@spring-projects-issues
Copy link
Collaborator Author

Ferenc Gratzer commented

The sample project's created. However, the converter in this very little project without any security, interceptors etc. works fine.
The project the issue happens is much more complex.

On Friday I debugged the startup of the application and the converters seemed to be registered properly in GenericConversionService.
On Monday I'll look into the other end, into the DataBinder.

Any other idea where and what to look for to identify the cause of the issue?

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

pull request: spring-attic/spring-framework-issues#19

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

I would put a breakpoint in AnnotationMethodHandlerAdapter when it's about to process the request (e.g. in invokeHandlerMethod()) and examine the webBindingInitializer property and the ConversionService it contains, does it have the custom converter? If not then there is some configuration you need to trace. If yes, then I would debug the call to the conversion service to see why it fails to find the converter even though it knows about it.

@spring-projects-issues
Copy link
Collaborator Author

Ferenc Gratzer commented

The demo for the issue is created.

I think the issue is on the line 432 in GenericConversionService.ConverterAdapter:
return this.typeInfo.getTargetType().equals(targetType.getObjectType());

In my view it should be:
return this.typeInfo.getTargetType().isAssignableFrom(targetType.getObjectType());

If you compare the version of 3.0.6 and 3.0.7 of GenericConversionService, you'll see that those lines were only added in 3.0.7.

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

I see now. The converter is from String to Environment but the actual target object is a subtype of Environment. I think not allowing this is intentional since a Converter doesn't know what the actual target type expected is, it may return one that won't match. I I'll try to dig up the issue where this was change was made.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Feb 29, 2012

Rossen Stoyanchev commented

The issue where this was addressed is #13360. I guess you'll need to use GenericConverter for something like that or depending on your needs perhaps a ConverterFactory.

@spring-projects-issues spring-projects-issues added type: bug A general bug status: declined A suggestion or change that we don't feel we should currently apply in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug A general bug label Jan 12, 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
Projects
None yet
Development

No branches or pull requests

2 participants