描述访问网络资源的格式
传统风格:http://localhost/user/saveUser
rest风格:http://localhost/user
优点
1.隐藏资源访问行为(用行为动作区分操作)
2.书写简化
注解解释
@RequestMapping
@PathVariable
@RestController
简化开发注解
入门案例(最基础,有不合理)
@RequestMapping(value = "/users", method = RequestMethod.POST)@ResponseBodypublic String save(@RequestBody User user) {System.out.println("user save " + user);return "{'module':'user save'}";}@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)@ResponseBodypublic String delete(@PathVariable Integer id) {System.out.println("user delete " + id);return "{'module':'user delete'}";}@RequestMapping(value = "/users", method = RequestMethod.PUT)@ResponseBodypublic String update(@RequestBody User user) {System.out.println("user update " + user);return "{'module':'user update'}";}@RequestMapping(value = "/users/{id}",method = RequestMethod.GET)@ResponseBodypublic String getById(@PathVariable Integer id) {System.out.println("user getById " + id);return "{'module':'user getById'}";}@RequestMapping(value = "/users",method = RequestMethod.GET)@ResponseBodypublic String getAll() {System.out.println("user getAll ");return "{'module':'user getAll'}";}
快速开发
package org.example.controller;/** @Auther:huangzhiyang* @Date:2023/10/7* @Description:*/import org.example.domain.Book;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/book")
public class BookController {@PostMappingpublic String save(@RequestBody Book book) {System.out.println("book save "+book);return "{'info':'springmvc'}";}@DeleteMapping("/{id}")public String delete( @PathVariable String id) {System.out.println("book delete"+id);return "{'info':'springmvc'}";}@PutMappingpublic String update(@RequestBody Book book) {System.out.println("book update");return "{'info':'springmvc'}";}
}