学习目标:
RestFul风格内容
学习内容:
RestFul风格内容
学习时间:
学习产出:
接上篇RequesMappingRestFul风格(简洁、高效、安全)
概念
RestFul就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
功能
- 资源:互联网所有的事物都可以被抽象为资源
- 资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。
- 分别对应 添加、 删除、修改、查询。
传统方式操作资源 :通过不同的参数来实现不同的效果!方法单一,post 和 get
- http://127.0.0.1/item/queryItem.action?id=1 查询,GET
- http://127.0.0.1/item/saveItem.action 新增,POST
- http://127.0.0.1/item/updateItem.action 更新,POST
- http://127.0.0.1/item/deleteItem.action?id=1 删除,GET或POST
使用RestFul操作资源 : 可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能可以不同!
- http://127.0.0.1/item/1 查询,GET
- http://127.0.0.1/item 新增,POST
- http://127.0.0.1/item 更新,PUT
- http://127.0.0.1/item/1 删除,DELETE
(1)、传统模式
Controller
@Controller
public class RestFulController {
//传统方式:http://localhost:8080/add?a=1&b=2
@RequestMapping("/add")
public String testRestFul(int a, int b, Model model){
int result = a+b;
model.addAttribute("msg","结果为:"+result);
return "test";
}
}
springmvc-servlet.xml、web.xml、test.jsp都没改
访问路径为:http://localhost:8080/add?a=1&b=2
(2)、RestFul风格
Controller
@Controller
public class RestFulController {
//传统方式:http://localhost:8080/add?a=1&b=2
//RestFul风格:http://localhost:8080/add/1/2
//映射访问路径
@RequestMapping("/add/{a}/{b}")
public String testRestFul(@PathVariable int a,@PathVariable int b, Model model){
int result = a+b;
model.addAttribute("msg","结果为:"+result);
return "test";
}
}
springmvc-servlet.xml、web.xml、test.jsp都没改
访问路径为:http://localhost:8080/add/1/2
RestFul风格使url的参数不对外显示,使更加安全
- 使路径变得更加简洁;
- 获得参数更加方便,框架会自动进行类型转换。
- 通过路径变量的类型可以约束访问参数,如果类型不一样,则访问不到对应的请求方法,假设访问的路径是/add/1/a,则显示400路径与方法不匹配(将int b改为String b则正确),而不会是参数转换失败。
(3)、使用method属性指定请求类型
Controller
@Controller
public class RestFulController {
//传统方式:http://localhost:8080/add?a=1&b=2
//RestFul风格:http://localhost:8080/add/1/2
//映射访问路径
@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.DELETE)
public String testRestFul(@PathVariable int a,@PathVariable int b, Model model){
int result = a+b;
model.addAttribute("msg","结果为:"+result);
return "test";
}
}
其他都不变
显示Request method ‘GET’ not supported,这是由于我们使用浏览器地址栏进行访问默认是Get请求,因此会报错405
将method改为 Get,访问正常:
@Controller
public class RestFulController {
//传统方式:http://localhost:8080/add?a=1&b=2
//RestFul风格:http://localhost:8080/add/1/2
//映射访问路径(value也可以使用path代替)
@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
public String testRestFul(@PathVariable int a,@PathVariable int b, Model model){
int result = a+b;
model.addAttribute("msg","结果为:"+result);
return "test";
}
}
Spring MVC 的 @RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。
所有的地址栏请求默认都会是 HTTP GET 类型的。
(4)、使用不同的方法访问不同的内容
添加一个a.jsp用于使用post方法
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/add/1/3" method="post">
<input type="submit">
</form>
</body>
</html>
Controller
@Controller
public class RestFulController {
//传统方式:http://localhost:8080/add?a=1&b=2
//RestFul风格:http://localhost:8080/add/1/2
//映射访问路径
@GetMapping("/add/{a}/{b}")//Get方法走这个(浏览器地址栏默认)
public String testRestFul1(@PathVariable int a,@PathVariable int b, Model model){
int result = a+b;
model.addAttribute("msg","结果为get:"+result);
return "test";
}
@PostMapping("/add/{a}/{b}")//Post方法走这个(表单提交数据)
public String testRestFul2(@PathVariable int a,@PathVariable int b, Model model){
int result = a+b;
model.addAttribute("msg","结果为post:"+result);
return "test";
}
}
地址:http://localhost:8080/add/1/2 页面显示结果为get:3
地址:http://localhost:8080/add/1/3 页面显示结果为post:4