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的错误
重新粘贴一下代码,web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
contextConfigLocation
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/rest-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/restful/*</url-pattern>
</servlet-mapping>
6080端口,,写错了吗?
前端没有渲染,添加依赖<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>