/** * Rest和Restful的区别: * -Rest是一种系统架构 * -Restful是一种接口规则 * -RestController是一组接口规则 * 关系:Restful的API是符合Rest架构的API(GET/POST/PUT/DELETE) * RestController的API是符合Rest架构的API(返回结果是JSON) * * Rest就是对资源请求形式的一种规范 * 1、用URI定位资源 * 2、URI由名称组成 * 3、使用HTTP方法对应数据的操作 * * URI(Universal Resource Identifier):统一资源标志符(目前HTTP规范使用URI) * URL(Universal Resource Locator):统一资源定位符(如:https://www.biadu.com/resourse001,可以定位资源) * URN(Universal Resource Name):统一资源名称(如:isbn:0-395-36341-1,可以定位唯一书本) * 其中,URI包含了URL和URN两个类别 * * 常用HTTP请求类型 * GET:获取资源(Read) * POST:创建资源(Create) * PUT:更新资源(Update) * DELETE :删除资源(Delete) * * Spring MVC提供了对Restful风格的API支持 * Spring MVC相关注解 * @RequestMapping:接收指定method类型请求(默认通用)和value/path映射的URL(默认"/") * * @GetMapping:接收GET请求指定的URL * @PostMapping:接收POST请求指定的URL * @PutMapping:接收PUT请求的指定URL * @DeleteMapping:接收DELETE请求指定的URL * * @RequestParam:必须在请求的URL/body中带有该参数 * @RequestBody:必须在请求的body中带有JSON格式参数 * @ReponseBody:返回JSON格式文本 * * 其中, * @RestController=@Controller+@ResponseBody * @PostMapping=@RequestMapping(method=RequestMethod.POST) * GET/PUT/DELETE具有幂等性,POST不具有幂等性(幂等性:一次请求和多次请求时,结果是一致的) */
package com.xch.controller1;import org.springframework.web.bind.annotation.*;/** * 测试Restful风格API * * @author Chenghe Xu * @date 2023/2/3 15:24 */@RequestMapping("/restful")@RestControllerpublic class Test01 {//@GetMapping("getapi")@RequestMapping(method = RequestMethod.GET, path = "getapi")public String getTest() {String str = "==Read获取用户信息操作==";System.out.println(str);return str;}//@PostMapping("postapi")@RequestMapping(method = RequestMethod.POST, path = "postapi")public String postTest() {String str = "==Create创建用户信息操作==";System.out.println(str);return str;}//@PutMapping("putapi")@RequestMapping(method = RequestMethod.PUT, path = "putapi")public String putTest() {String str = "==Update更新用户信息操作==";System.out.println(str);return str;}//@DeleteMapping("deleteapi")@RequestMapping(method = RequestMethod.DELETE, path = "deleteapi")public String deleteTest() {String str = "==Delete删除用户信息操作==";System.out.println(str);return str;}//默认RequestMapping声明的接口,是通用接口,支持GET/POST/PUT/DELETE/OTHERS@RequestMapping("generalapi")public String generalTest() {String str = "==通用用户信息操作==";System.out.println(str);return str;}}