Announcement Announcement Module
Collapse
No announcement yet.
3.1.2 -> 3.2 issue with ContentNegotiatingViewResolver. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 3.1.2 -> 3.2 issue with ContentNegotiatingViewResolver.

    I'm getting a ClassCastException after I update from Spring 3.1.2 to 3.2. This works with 3.1.2. It's possible I missed something in the release notes? Any thoughts?

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Invocation of init method failed; nested exception is java.lang.ClassCastException: java.lang.String cannot be cast to org.springframework.http.MediaType
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    	at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
    	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
    	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
    	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
    	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
    	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
    	at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1206)
    	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1026)
    	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4421)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4734)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.springframework.http.MediaType
    	at org.springframework.web.accept.MappingMediaTypeFileExtensionResolver.<init>(MappingMediaTypeFileExtensionResolver.java:56)
    	at org.springframework.web.accept.AbstractMappingContentNegotiationStrategy.<init>(AbstractMappingContentNegotiationStrategy.java:42)
    	at org.springframework.web.accept.PathExtensionContentNegotiationStrategy.<init>(PathExtensionContentNegotiationStrategy.java:74)
    	at org.springframework.web.accept.ServletPathExtensionContentNegotiationStrategy.<init>(ServletPathExtensionContentNegotiationStrategy.java:47)
    	at org.springframework.web.accept.ContentNegotiationManagerFactoryBean.afterPropertiesSet(ContentNegotiationManagerFactoryBean.java:166)
    	at org.springframework.web.servlet.view.ContentNegotiatingViewResolver.afterPropertiesSet(ContentNegotiatingViewResolver.java:270)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
    	... 33 more
    Code:
    <beans:bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    	<beans:property name="mediaTypes">
    		<beans:map>
    			<beans:entry key="html" value="text/html" />
    			<beans:entry key="json" value="application/json" />
    		</beans:map>
    	</beans:property>
    	<beans:property name="viewResolvers">
    		<beans:list>
    			<beans:bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" />
    			<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    				<beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    				<beans:property name="prefix" value="/WEB-INF/views/" />
    				<beans:property name="suffix" value=".jsp" />
    			</beans:bean>
    		</beans:list>
    	</beans:property>
    	<beans:property name="defaultViews">
    		<beans:list>
    			<beans:bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    		</beans:list>
    	</beans:property>
    </beans:bean>

  • #2
    Hi sbirnie,

    I'm also having the same problem. I upgraded to 3.2 to get access to packagesToScan in JAXB2Marshaller, but then had the same error as you.

    I was digging a bit, but not much resource on that. All I know for now is that the method has been deprecated, and that we have to use setContentNegotiationManager(ContentNegotiationMan ager) instead. Seems the responsibility of finding the correct mediaType has move to there, but no example on how to build it, I'll try to look by myself...

    Found that in the javadoc for the new ContentNegotiatingViewResolver:
    http://static.springsource.org/sprin...wResolver.html

    Hope it helps! I will post an example if I can find it!

    Regards,

    Laurent.

    Comment


    • #3
      I have the same problem...anybody has resolved?

      Thx!
      Ranob
      Originally posted by luolunci View Post
      Hi sbirnie,

      I'm also having the same problem. I upgraded to 3.2 to get access to packagesToScan in JAXB2Marshaller, but then had the same error as you.

      I was digging a bit, but not much resource on that. All I know for now is that the method has been deprecated, and that we have to use setContentNegotiationManager(ContentNegotiationMan ager) instead. Seems the responsibility of finding the correct mediaType has move to there, but no example on how to build it, I'll try to look by myself...

      Found that in the javadoc for the new ContentNegotiatingViewResolver:
      http://static.springsource.org/sprin...wResolver.html

      Hope it helps! I will post an example if I can find it!

      Regards,

      Laurent.

      Comment


      • #4
        Hi guys,

        this seems to work for me:

        Code:
            <!-- REST -->
            <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
                <property name="order" value="1" />
                <property name="contentNegotiationManager">
                    <bean class="org.springframework.web.accept.ContentNegotiationManager">
                        <constructor-arg>
                            <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
                                <constructor-arg>
                                    <map>
                                        <entry key="json" value="application/json"/>
                                        <entry key="xml" value="application/xml"/>
                                    </map>
                                </constructor-arg>
                            </bean>
                        </constructor-arg>
                    </bean>
                </property>
        
        
                <property name="defaultViews">
                    <list>
                        <!-- JSON View -->
                        <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        
                        <!-- XML View -->
                        <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                            <constructor-arg>
                                <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                                    <property name="packagesToScan">
                                        <list>
                                            <value>documentLoader.domain</value>
                                        </list>
                                    </property>
                                </bean>
                            </constructor-arg>
                        </bean>
                    </list>
                </property>
            </bean>
        
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                <property name="prefix" value="/WEB-INF/views/" />
                <property name="suffix" value=".jsp" />
            </bean>
        Regards,

        Laurent.

        Comment


        • #5
          Thx Laurent!!

          Originally posted by luolunci View Post
          Hi guys,

          this seems to work for me:

          Code:
              <!-- REST -->
              <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
                  <property name="order" value="1" />
                  <property name="contentNegotiationManager">
                      <bean class="org.springframework.web.accept.ContentNegotiationManager">
                          <constructor-arg>
                              <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
                                  <constructor-arg>
                                      <map>
                                          <entry key="json" value="application/json"/>
                                          <entry key="xml" value="application/xml"/>
                                      </map>
                                  </constructor-arg>
                              </bean>
                          </constructor-arg>
                      </bean>
                  </property>
          
          
                  <property name="defaultViews">
                      <list>
                          <!-- JSON View -->
                          <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
          
                          <!-- XML View -->
                          <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                              <constructor-arg>
                                  <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                                      <property name="packagesToScan">
                                          <list>
                                              <value>documentLoader.domain</value>
                                          </list>
                                      </property>
                                  </bean>
                              </constructor-arg>
                          </bean>
                      </list>
                  </property>
              </bean>
          
              <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                  <property name="prefix" value="/WEB-INF/views/" />
                  <property name="suffix" value=".jsp" />
              </bean>
          Regards,

          Laurent.

          Comment


          • #6
            In addition, spring-oxm dependency is required to make Laurent's solution work.

            Thanks!
            Originally posted by ranob View Post
            Thx Laurent!!

            Comment


            • #7
              Created a Jira (SPR-10119) for this issue:

              https://jira.springsource.org/browse/SPR-10119

              Comment


              • #8
                Hi everybody,
                I'm experiencing a very strange problem.
                I have the same problem. Were you able to solve it? if so, how? iam running a spring application with spring 3.1.X. but it throws me a

                java.lang.NoClassDefFoundError:org/springframework/beans/factory/NoUniqueBeanDefinitionException when i try to start the server. According to the docs, this exception has been introduced in version 3.2.x of the Spring framework. And that's the weird thing: the project uses version 3.1.x!
                I did not add nor remove any dependency.

                When i see the dependencies, i see a 3.2.1.RELEASE in the spring-orm module. I tried cleaning project, and re building it, but it doesnt work. What might be the problem. I am stuck on this issue for a week now!

                Any suggestions?

                Thanks in advance,
                Rajeev

                Comment


                • #9
                  It very strange. When i try to access page for example test.jsp the tomcat print the page as it is. and dont parse any JSTL tags $( Why?

                  Comment


                  • #10
                    The problem was:
                    Code:
                    <servlet-mapping>
                            <servlet-name>restServlet</servlet-name>
                            <url-pattern>/*</url-pattern>
                        </servlet-mapping>
                    Replaced by:
                    Code:
                    <servlet-mapping>
                            <servlet-name>restServlet</servlet-name>
                            <url-pattern>/</url-pattern>
                        </servlet-mapping>

                    Comment

                    Working...
                    X