很多RESTFUL风格的访问地址都是一样的,只是行为动作区分了,对外隐藏了真实操作
代码示例
/**
* @author hrui
* @date 2024/8/9 14:26
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
private Integer id;
private String name;
}
package com.example.demo1.controller;
import com.example.demo1.pojo.Book;
import org.springframework.web.bind.annotation.*;
/**
* @author hrui
* @date 2024/8/8 22:53
*/
@RestController
@RequestMapping("/books")
public class TestController {
@GetMapping("/{id}")
public String getBookById(@PathVariable Integer id){
return "获得Id为 " + id+ " 的图书";
}
@GetMapping("/{id}/{name}")
public String getBookByIdAndName(@PathVariable Integer id,@PathVariable String name){
return "获得Id为 " + id+ " 的图书,name为"+name;
}
@GetMapping("")
public String getAllBooks(){
return "获得所有图书";
}
@PostMapping("")
public String saveBook(@RequestBody Book book){
return "新增图书成功";
}
@PutMapping("")
public String updateBook(@RequestBody Book book){
return "修改图书成功";
}
@DeleteMapping("/{id}")
public String deleteBook(@PathVariable Integer id){
return "删除id为 "+id+" 图书成功";
}
}