SpringBoot :cannot be cast to org.springframework.web.accept.ContentNegotiationManager

SpringBoot配置攔截器時,始終報錯:web

cannot be cast to org.springframework.web.accept.ContentNegotiationManagerspring

啓動入口:spring-mvc

@EnableAutoConfiguration
@SpringBootApplication
@ImportResource("classpath:application-mvc.xml")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
        registration.addUrlMappings("*.do", "*.go");
        return registration;
    }
}

Interceptor:mvc

public class CustomSSOSpringInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println();
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

}

配置攔截器:app

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CustomSSOSpringInterceptor()).addPathPatterns("/**");
    }
}

 

沒有問題啊?但是始終報錯,網上找了不少資料,都沒有一個具體的說法,最後在一個小回復裏看到了報錯的緣由:ide

http://forum.spring.io/forum/spring-projects/web/124660-spring-3-2-classcastexception-contentnegotiationmanagerfactorybeanpost

I finally found the error, I was including a xml-config with <mvc:annotation-driven/>... I removed this, now it worksthis

原來是我在application.xml文件裏面配置了以下東西:spa

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"
       default-autowire="byName">

    <mvc:annotation-driven/>

    <bean class="com.ahoi.demo.common.interceptor.ControllerClassNameHandlerMapping"></bean>

</beans>

註釋掉就能夠了!xml

相關文章
相關標籤/搜索