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

Conversion from String to MediaType should not fail with default ConversionService [SPR-7282] #11941

Closed
spring-projects-issues opened this issue Jun 13, 2010 · 3 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Gerrit Brehmer opened SPR-7282 and commented

Because MediaType has an special constructor with only one String parameter, the ObjectToObjectConverter thought, that he can convert the String value "application/xml" to an MediaType. So ObjectToObjectConverter will call the constructor and conversion fails, because this constructor only need the primary content type:

Exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contentNegotiatingViewResolver' defined in ServletContext resource [/WEB-INF/app-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value application/xml from type 'java.lang.String' to type 'org.springframework.http.MediaType'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value application/xml from type 'java.lang.String' to type 'org.springframework.http.MediaType'; nested exception is java.lang.reflect.InvocationTargetException
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
	at javax.servlet.GenericServlet.init(GenericServlet.java:212)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1139)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:966)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3996)
	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4266)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
	at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
	at org.apache.catalina.core.StandardService.start(StandardService.java:448)
	at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:592)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Caused by: org.springframework.core.convert.ConversionFailedException: Unable to convert value application/xml from type 'java.lang.String' to type 'org.springframework.http.MediaType'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value application/xml from type 'java.lang.String' to type 'org.springframework.http.MediaType'; nested exception is java.lang.reflect.InvocationTargetException
	at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:40)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:138)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:201)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:155)
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:461)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1363)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1322)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1076)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	... 31 more
Caused by: org.springframework.core.convert.ConversionFailedException: Unable to convert value application/xml from type 'java.lang.String' to type 'org.springframework.http.MediaType'; nested exception is java.lang.reflect.InvocationTargetException
	at org.springframework.core.convert.support.ObjectToObjectConverter.convert(ObjectToObjectConverter.java:82)
	at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:37)
	... 39 more
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:501)
	at org.springframework.core.convert.support.ObjectToObjectConverter.convert(ObjectToObjectConverter.java:70)
	... 40 more
Caused by: java.lang.IllegalArgumentException: Invalid token character '/' in token "application/xml"
	at org.springframework.http.MediaType.checkToken(MediaType.java:286)
	at org.springframework.http.MediaType.<init>(MediaType.java:257)
	at org.springframework.http.MediaType.<init>(MediaType.java:207)
	at org.springframework.http.MediaType.<init>(MediaType.java:196)
	... 45 more

With disabled ConversionService, the value will be converted by MediaTypeEditor, which uses the MediaType.parseMediaType method.
I think the behauvior should be the same like mediaTypes-parameter map (ContentNegotiatingViewResolver), where Strings must be used.

Example spring config:

<bean id="contentNegotiatingViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="defaultContentType" value="application/xml"/>
  <property name="mediaTypes">
    <map>
      <entry key="xml" value="application/xml" />
      <entry key="json" value="application/json" />
    </map>
  </property>
</bean>

Affects: 3.0.2

Referenced from: commits 96b1dc9

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Aside from the specific MediaType issue, there is definitely an issue with error reporting here since the InvocationTargetException shouldn't be visible in that stacktrace. We'll have a quick look at this for 3.0.3 still.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Fixed through the addition of a 'valueOf' method, simply delegating to the existing parseMediaType method. The 'valueOf' convention automatically overrides the MediaType(String) constructor according to ConversionService rules.

Also fixed the InvocationTargetException problem, wrapping the underlying exception directly now.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Rajesh commented

This has resurfaced in 3.1.3.RELEASE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants