web.xml配置如下:
rest
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/rest-servlet.xml
1
rest
/restful/*
rest-servlet.xml配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<description>Spring MVC for Restful</description>
<!--检测注解 -->
<context:component-scan base-package="com.onlyyou.java.controller" />
<context:annotation-config />
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
</beans>
Controller代码如下:
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(value="/user", method= RequestMethod.GET)
@ResponseBody
public UserBean testParam(@RequestParam("username") String name) {
UserBean userBean = new UserBean();
userBean.setAvatar("123");
userBean.setBirthday("323");
userBean.setCensusRegister("dfda");
return userBean;
}
}
最后再浏览器访问http://localhost:6080/onlyyou/restful/hello/user?username=tom报404的错误