我正在尝试使用Spring 4创建一个Rest控制器。我还使用mongodb作为数据库,使用Tomcat 7作为webserver。 当我尝试提交任何Rest请求时,我收到此错误消息:
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>
这是使用的控制器类:
@RestController
@RequestMapping("/rest/user")
public class UserContoller {
@Autowired
UserServices userServices;
@RequestMapping(value = "/{username}", method = RequestMethod.GET)
public @ResponseBody User findUserByUsername(@PathVariable ("username") String username){
return userServices.findUserByUsername(username);
}
}
你能帮帮我吗?
提前谢谢你
答案 0 :(得分:0)
我有一个类似的问题,我相信一系列事情解决了这个问题:
确保servlet.xml中有以下两行并按此顺序排列(意味着必须在default-servlet-handler之前列出注释驱动)
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
*任何等效的viewResolver bean都可以。
确保它位于您的web.xml中:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
我看到你没有提到你GET会产生什么。例如:我的@RequestMapping produces = MediaType.APPLICATION_JSON_VALUE
最后,我不确定为什么,但每次添加新的控制器映射时,我都需要运行Maven Install生命周期,或者它没有看到新的映射。这个问题我还在努力解决。
答案 1 :(得分:0)
我这样解决了:
@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);
}