欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

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

发布时间:2025/3/15 javascript 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 SpringBoot :cannot be cast to org.springframework.web.accept.ContentNegotiationManager 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘Python工程师标准>>>

SpringBoot配置拦截器时,始终报错:

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

启动入口:

@EnableAutoConfiguration @SpringBootApplication @ImportResource("classpath:application-mvc.xml") public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}@Beanpublic ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);registration.addUrlMappings("*.do", "*.go");return registration;} }

Interceptor:

public class CustomSSOSpringInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println();return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}}

配置拦截器:

@Configuration public class WebMvcConfig extends WebMvcConfigurationSupport {@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new CustomSSOSpringInterceptor()).addPathPatterns("/**");} }

 

没有问题啊?可是始终报错,网上找了很多资料,都没有一个具体的说法,最后在一个小回复里看到了报错的原因:

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

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

原来是我在application.xml文件里面配置了如下东西:

<?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.xsdhttp://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>

注释掉就可以了!

转载于:https://my.oschina.net/u/3047936/blog/1806406

总结

以上是生活随笔为你收集整理的SpringBoot :cannot be cast to org.springframework.web.accept.ContentNegotiationManager的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。