Mongodb 无法初始化类org.springframework.web.context.request.ServletRequestAttributes
我正在尝试使用Spring4创建一个Rest控制器。我还使用mongodb作为数据库,使用Tomcat7作为Web服务器。 当我尝试提交任何Rest请求时,收到此错误消息:Mongodb 无法初始化类org.springframework.web.context.request.ServletRequestAttributes,mongodb,spring-mvc,Mongodb,Spring Mvc,我正在尝试使用Spring4创建一个Rest控制器。我还使用mongodb作为数据库,使用Tomcat7作为Web服务器。 当我尝试提交任何Rest请求时,收到此错误消息: type Exception report message Servlet execution threw an exception description The server encountered an internal error that prevented it from fulfilling thi
type Exception report
message Servlet execution threw an exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoClassDefFoundError: Could not initialize class org.springframework.web.context.request.ServletRequestAttributes
org.springframework.web.servlet.FrameworkServlet.buildRequestAttributes(FrameworkServlet.java:996)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:923)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.56 logs.
我正在pom.xml中使用这些版本
<!-- Web -->
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<servlet.version>2.5</servlet.version>
<!-- Spring -->
<spring-framework.version>4.0.0.RELEASE</spring-framework.version>
<!-- spring-web -->
<spring-web.version>4.1.2.RELEASE</spring-web.version>
<!-- Spring data for MongoDB -->
<springframework.data-version>1.6.1.RELEASE</springframework.data-version>
你能帮帮我吗?
提前谢谢你我也遇到过类似的问题,我相信通过多种方法解决了这个问题:
- 确保servlet.xml中有以下两行,并按此顺序排列(这意味着必须在默认servlet处理程序之前列出注释驱动)
- 请确保这是在您的web.xml中:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
org.springframework.web.context.ContextLoaderListener
- 我知道你没有提到你得到的东西会产生什么。例如:我在@RequestMapping
products=MediaType.APPLICATION\u JSON\u VALUE
- 最后,我不知道为什么,但每次添加新的控制器映射时,我都需要运行Maven安装生命周期,否则它就看不到新的映射。这个问题我还在想办法解决
- 我是这样修复的:
@RequestMapping(value = "/user", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public User findUsername(@RequestParam("username") String username) {
logger.info("Searcing for user using his username");
return userServices.findUserByUsername(username);
}
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
@RequestMapping(value = "/user", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public User findUsername(@RequestParam("username") String username) {
logger.info("Searcing for user using his username");
return userServices.findUserByUsername(username);
}