Spring MVC Content Negotiation throwing ClassCastException

Lurk21

Following this Spring tutorial:

https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

I added the following class to my controller package:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false).
            favorParameter(true).
            parameterName("mediaType").
            ignoreAcceptHeader(true).
            useJaf(false).
            defaultContentType(MediaType.APPLICATION_JSON).
            mediaType("xml", MediaType.APPLICATION_XML).
            mediaType("json", MediaType.APPLICATION_JSON);
  }
}

And I started receiving the following stack trace:

java.lang.ClassCastException: org.springframework.web.accept.ContentNegotiationManagerFactoryBean$$EnhancerByCGLIB$$945e0e59 cannot be cast to org.springframework.web.accept.ContentNegotiationManager
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e8097a54.mvcContentNegotiationManager(<generated>)
    org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addDefaultHandlerExceptionResolvers(WebMvcConfigurationSupport.java:632)
    org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.handlerExceptionResolver(WebMvcConfigurationSupport.java:596)
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e8097a54.CGLIB$handlerExceptionResolver$14(<generated>)
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e8097a54$$FastClassBySpringCGLIB$$89c6148d.invoke(<generated>)
    org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:293)
    org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e8097a54.handlerExceptionResolver(<generated>)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:160)
    org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1055)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:951)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:602)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:521)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:462)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:183)
    org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:95)
    org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.internalProcess(ActiveRequestResponseCacheValve.java:74)
    org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:47)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:599)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:451)
    java.lang.Thread.run(Thread.java:662)
Lurk21

I went with the XML configuration (as documented in the tutorial) as opposed to the java configuration and it worked.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring boot controller content negotiation

From Dev

How to Use Content Negotiation in Spring Data Rest?

From Dev

How to disable content negotiation for Spring Actuators?

From Dev

Is there a simple way to solve content negotiation in Spring boot

From Dev

How to Use Content Negotiation in Spring Data Rest?

From Dev

spring cloud config-server .properties content-negotiation failing

From Dev

Is there better approach to Accept Header based content negotiation in Spring API?

From Dev

How to add csv MimeType to spring content-negotiation?

From Dev

Content negotiation to return HTML

From Dev

PhalconPHP: content negotiation?

From Dev

Restlet Content Type Negotiation

From Dev

Content negotiation using WebSharper

From Dev

PhalconPHP: content negotiation?

From Dev

Spring MVC @PathVariable with url address throwing error

From Dev

MysqlDataSource throwing ClassCastException in Glassfish

From Dev

jdbcTemplate batchUpdate is throwing ClassCastException

From Dev

Jersey - content negotiation for error handling

From Dev

Empty content in spring mvc test

From Dev

java.lang.LinkageError: ClassCastException: in spring MVC project

From Dev

Using @Valid is throwing exceptions & not working in basic Spring 3.0 MVC program

From Dev

Using @Valid is throwing exceptions & not working in basic Spring 3.0 MVC program

From Dev

Spring MVC 4.1.x RestFul Service throwing Not Acceptable for Json

From Dev

SupportedMediaTypes not used when doing content negotiation

From Dev

Why not use content negotiation to return JSON object?

From Dev

Content Negotiation when looking at different output formats

From Dev

Content negotiation for File Download In Web API

From Dev

Content negotiation not working with respond_to stanza

From Dev

What is performing Transparent Content Negotiation in Apache

From Dev

Refreshing static content with Spring MVC and Boot

Related Related

  1. 1

    Spring boot controller content negotiation

  2. 2

    How to Use Content Negotiation in Spring Data Rest?

  3. 3

    How to disable content negotiation for Spring Actuators?

  4. 4

    Is there a simple way to solve content negotiation in Spring boot

  5. 5

    How to Use Content Negotiation in Spring Data Rest?

  6. 6

    spring cloud config-server .properties content-negotiation failing

  7. 7

    Is there better approach to Accept Header based content negotiation in Spring API?

  8. 8

    How to add csv MimeType to spring content-negotiation?

  9. 9

    Content negotiation to return HTML

  10. 10

    PhalconPHP: content negotiation?

  11. 11

    Restlet Content Type Negotiation

  12. 12

    Content negotiation using WebSharper

  13. 13

    PhalconPHP: content negotiation?

  14. 14

    Spring MVC @PathVariable with url address throwing error

  15. 15

    MysqlDataSource throwing ClassCastException in Glassfish

  16. 16

    jdbcTemplate batchUpdate is throwing ClassCastException

  17. 17

    Jersey - content negotiation for error handling

  18. 18

    Empty content in spring mvc test

  19. 19

    java.lang.LinkageError: ClassCastException: in spring MVC project

  20. 20

    Using @Valid is throwing exceptions & not working in basic Spring 3.0 MVC program

  21. 21

    Using @Valid is throwing exceptions & not working in basic Spring 3.0 MVC program

  22. 22

    Spring MVC 4.1.x RestFul Service throwing Not Acceptable for Json

  23. 23

    SupportedMediaTypes not used when doing content negotiation

  24. 24

    Why not use content negotiation to return JSON object?

  25. 25

    Content Negotiation when looking at different output formats

  26. 26

    Content negotiation for File Download In Web API

  27. 27

    Content negotiation not working with respond_to stanza

  28. 28

    What is performing Transparent Content Negotiation in Apache

  29. 29

    Refreshing static content with Spring MVC and Boot

HotTag

Archive