Rest架构下的Restful风格API相关注解+测试用例

/**
 * 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")
@RestController
public 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;
    }
    
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BB-X

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值