restful风格入门
概述
简介:REST(Representational State Transfer),表现形式状态转换。
-
传统风格
http://localhost/users/getById?id=1
http://localhost/users/saveUser -
REST风格
http://localhost/users/1
http://localhost/users
对比:
对比可以发现,虽然传统风格的更详细,知道调用哪个函数,但是了太长了写起来不方便,REST风格的话就很简介,而且还可以隐藏函数调用的函数的名字,无法通过地址得知对资源进行何种操作,更加安全。
如何区分资源访问形式
在后面的代码演示的时候就会知道,REST风格定义了很多种类的Mapping注解,通过这些注解进行识别。
- http://localhost/users 查询全部用户信息 GET(查询)
- http://localhost/users/1 查询指定用户信息 GET(查询)
- http://localhost/users 添加用户信息 POST(添加/保存)
- http://localhost/users 修改用户信息 PUT(修改/更新)
- http://localhost/users 删除用户信息 DELETE(删除)
规范
REST风格更多的是一种规范,对于描述模块名称通常使用复数,比如users。
快速入门
文件结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringBootRestful</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootRestful</name>
<description>SpringBootRestful</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
User
package com.example.springbootrestful.bean;
public class User {
private Integer id;
}
Controller
传统代码
package com.example.springbootrestful.controller;
import com.example.springbootrestful.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
// save与update需要的是post方法才可以访问
@RequestMapping("/save")
@ResponseBody
public String save(@RequestBody User user){
System.out.println("user save..." + user);
return "{'module':'user save'}";
}
@RequestMapping("/delete")
@ResponseBody
public String delete(Integer id){
System.out.println("user delete..." + id);
return "{'module':'user delete'}";
}
@RequestMapping("/update")
@ResponseBody
public String update(@RequestBody User user){
System.out.println("user update..." + user);
return "{'module':'update save'}";
}
@RequestMapping("/getById")
@ResponseBody
public String getById(Integer id){
System.out.println("user getById..." + id);
return "{'module':'user getById'}";
}
@RequestMapping("/getAll")
@ResponseBody
public String getAll(){
System.out.println("user getAll...");
return "{'module':'user getAll'}";
}
}
运行演示:
RESTful风格代码
package com.example.springbootrestful.controller;
import com.example.springbootrestful.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class UserController {
// save与update需要的是post方法才可以访问
@RequestMapping(value = "/users",method = RequestMethod.POST)
@ResponseBody
public String save(@RequestBody User user){
System.out.println("user save..." + user);
return "{'module':'user save'}";
}
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id){
System.out.println("user delete..." + id);
return "{'module':'user delete'}";
}
@RequestMapping(value = "/users",method = RequestMethod.PUT)
@ResponseBody
public String update(@RequestBody User user){
System.out.println("user update..." + user);
return "{'module':'update save'}";
}
@RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
@ResponseBody
public String getById(@PathVariable Integer id){
System.out.println("user getById..." + id);
return "{'module':'user getById'}";
}
@RequestMapping(value = "/users",method = RequestMethod.GET)
@ResponseBody
public String getAll(){
System.out.println("user getAll...");
return "{'module':'user getAll'}";
}
}
完善
package com.example.springbootrestful.controller;
import com.example.springbootrestful.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/Users")
public class UserController {
// save与update需要的是post方法才可以访问
@PostMapping
public String save(@RequestBody User user){
System.out.println("user save..." + user);
return "{'module':'user save'}";
}
@DeleteMapping
public String delete(@PathVariable Integer id){
System.out.println("user delete..." + id);
return "{'module':'user delete'}";
}
@PutMapping
public String update(@RequestBody User user){
System.out.println("user update..." + user);
return "{'module':'update save'}";
}
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("user getById..." + id);
return "{'module':'user getById'}";
}
@GetMapping
public String getAll(){
System.out.println("user getAll...");
return "{'module':'user getAll'}";
}
}