Skip to content

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

@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions