文章目录
- 1. RESTful是什么?
- 2. 传统风格与RESTful风格对比
-
- 2.1 传统风格
- 2.1 RESTful风格
- 3. RESTful的实现
-
- 3.1 GET、POST方式
-
- 3.1.1 创建控制器类
- 3.1.2 创建一个jsp页面
- 3.2 PUT、DELETE方式
-
- 3.2.1 编写控制器方法
- 3.2.2 配置HiddenHttpMethodFilter
- 3.2.3 编写jsp页面
- 3.3 Http405 解决方法
1. RESTful是什么?
- RESTful 也称为REST(英文:Representational State Transfer)即表现层状态传递,它是一种软件架构风格或设计风格,而不是一个标准。
- REST 是Roy Fielding博士在2000年他的博士论文中提出来的。它是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。
2. 传统风格与RESTful风格对比
2.1 传统风格
如果是原来的架构风格,需要发送四个请求,分别是?
查询用户:http://localhost:8080/springmvc/selectuser?id=1
GET
增加用户: http://localhost:8080/springmvc/insertuser
POST
修改用户: http://localhost:8080/springmvc/updateuser
PUT
删除用户: http://localhost:8080/springmvc/deleteuser?id=1
DELETE
2.1 RESTful风格
按照传统方式发送请求的时候比较麻烦,需要定义多种请求,而RESTful在HTTP协议中,有不同的发送请求的方式,分别是GET、POST、PUT和DELETE方式,分别对应查询、修改、添加和删除操作。我们如果能让不同的请求方式表示不同的请求类型就可以简化我们的查询。
查询用户: http://localhost:8080/springmvc/user/1
GET 查询
查询多个用户: http://localhost:8080/springmvc/user
GET
添加用户: http://localhost:8080/springmvc/user
POST 添加
修改用户: http://localhost:8080/springmvc/user
PUT 修改
删除用户:http://localhost:8080/springmvc/user
DELETE 删除
注意:RESTful风格中的URL不存在动词形式的路径,如selectuser表示查询用户,是一个动词,要改为名词user。
3. RESTful的实现
RESTful 风格提倡URL地址使用统一的风格设计,各单词之间用斜杠分开。
3.1 GET、POST方式
3.1.1 创建控制器类
@Controller
public class UserController {
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String getAllUser(){
System.out.println("查询所有用户信息");
return "success";
}
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
public String getUserById(){
System.out.println("根据用户ID查询用户信息");
return "success";
}
@RequestMapping(value = "/user",method = RequestMethod.POST)
public String insertUser(String username,String password){
System.out.println("添加用户信息:" + username + ","+ password);
return "success";
}
}
3.1.2 创建一个jsp页面
- 通过超链接的方式进行测试
a href="${pageContext.request.contextPath}/user">查询全部/a>br>
a href="${pageContext.request.contextPath}/user/1">根据id查询信息/a>br>
form action="${pageContext.request.contextPath}/user" method="post">
用户名:input type="text" name="username">br>
密码:input type="password" name="password">br>
input type="submit" value="添加">br>
/form>
- 运行之后可以在控制台正常输出
3.2 PUT、DELETE方式
一切看起来都非常美好,但是大家需要注意了,我们在发送请求的时候只能发送post或者get,没有办法发送put和delete请求,那么应该如何处理呢?下面开始进入代码环节:
3.2.1 编写控制器方法
@RequestMapping(value = "/user",method = RequestMethod.PUT)
public String updateUser(String username,String password){
System.out.println("修改用户信息:" + username + ","+ password);
return "success";
}
@RequestMapping(value = "/user",method = RequestMethod.DELETE)
public String deleteUser(String username,String password){
System.out.println("删除用户信息:" + username + ","+ password);
return "success";
}
3.2.2 配置HiddenHttpMethodFilter
- 在web.xml文件中配置HiddenHttpMethodFilter过滤器来处理put和delete请求方式
filter>
filter-name>HiddenHttpMethodFilterfilter-name>
filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
filter-mapping>
filter-name>HiddenHttpMethodFilterfilter-name>
url-pattern>/*url-pattern>
filter-mapping>
3.2.3 编写jsp页面
处理put和delete请求方式注意事项:
- 请求方式必须为: post
- 请求参数必须为:_method
form action="${pageContext.request.contextPath}/user" method="post">
input name="_method" value="put" type="hidden"/>
用户名:input type="text" name="username">br>
密码:input type="password" name="password">br>
input type="submit" value="修改">br>
form>
form action="${pageContext.request.contextPath}/user" method="post">
input name="_method" value="delete" type="hidden"/>
用户名:input type="text" name="username">br>
密码:input type="password" name="password">br>
input type="submit" value="删除">br>
form>
测试结果:
3.3 Http405 解决方法
在处理put和delete请求方式时,可能会遇到这种情况:控制台能够正常输出,但是浏览器会报405错误
解决办法:
1.加入 @ResponseBody 注解。
2.请求先转给一个Controller,再返回jsp页面。
注意:注解添加位置在控制器方法处