restful、swagger与postman

本文深入探讨RESTful API的设计风格及其在移动互联网业务接口的应用,详细解析GET、POST、DELETE、PUT和PATCH等方法的用途。同时,介绍了Swagger工具在RESTful接口文档自动生成及功能测试中的应用,以及Postman在API调试方面的强大功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、restful

    restful不是一种新技术,而是一种编程风格,一种约定RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。RESTFUL适用于移动互联网厂商作为业务使能接口的场景,实现第三方OTT调用移动网络资源的功能,动作类型为新增、变更、删除所调用资源。

  •      GET   一般是用来做查询的,查询单个对象
    
  •      POST     一般用来做修改的
    
  •      DELETE   一般用来做删除
    
  •      PUT      一般用来做新增
    
  •      PATCH    一般用来操作批量数据的
    

例如:

@Controller
@RequestMapping("/department")//定位资源
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    //查询所有接口url,前台传递/department/list
    @RequestMapping(value = "/list",method = RequestMethod.PATCH)
    @ResponseBody
    public List<Department> findAll() {
        return departmentService.findAll();
    }
    //新增保存
    @RequestMapping(value = "/save" ,method = RequestMethod.PUT)
    @ResponseBody
    public AjaxResult save(@RequestBody Department department){
        try {
            departmentService.save(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,"保存失败");
        }
    }
    //删除接口url,前台传递/department/delete/1
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
    //修改
    @RequestMapping(value = "/update" ,method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestBody Department department){
        try {
            departmentService.update(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult(false,"保存失败");
        }
    }
    //查询单个
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Department findByID(@PathVariable("id") Long id){
        return departmentService.findOneById(id);
    }
}

2、swagger

    Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件,随着现在许多公司实现了前后端分离,swagger越来越受欢迎了。

2.1、jar包

<properties>
        <!--swagger对应的版本-->
        <springfox.version>2.4.0</springfox.version>
    </properties>
<dependencies>
	 <dependency>
	            <groupId>io.springfox</groupId>
	            <artifactId>springfox-swagger2</artifactId>
	            <version>${springfox.version}</version>
	        </dependency>
	        <dependency>
	            <groupId>io.springfox</groupId>
	            <artifactId>springfox-swagger-ui</artifactId>
	            <version>${springfox.version}</version>
	        </dependency>
</dependencies>        

2.2、SwaggerConfig

    在web层 新创建包com.sevens.crm.web.config和类:SwaggerConfig
SwaggerConfig

@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages="com.sevens.crm.web.controller")
public class SwaggerConfig {
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sevens.crm.web.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    
    private ApiInfo apiInfo(){
         @SuppressWarnings("deprecation")
        ApiInfo info=new ApiInfo(
                 "CRM所需要的全部接口",
                 "测试接口",
                 "1.0",
                 "www.sevens.com",
                 "seven",
                 "7",
                 "7");
         return info;
    }
}

在这里插入图片描述
在这里插入图片描述
不要忘记扫描包

<!--扫描swagger-->
    <context:component-scan base-package="com.sevens.crm.web.config"/>

2.3、运行

http://localhost/swagger-ui.html

2.4、效果

在这里插入图片描述

3、postman

    Postman 提供功能强大的 Web API 和 HTTP 请求的调试,它能够发送任何类型的HTTP 请求 (GET, POST, PUT, DELETE…),并且能附带任何数量的参数和 Headers。不仅如此,它还提供测试数据和环境配置数据的导入导出,付费的 Post Cloud 用户还能够创建自己的 Team Library 用来团队协作式的测试,并能够将自己的测试收藏夹和用例数据分享给团队。
官方下载:https://www.getpostman.com/

3.1、安装

下载之后打开需要注册
在这里插入图片描述

3.2、查询所有(无参)

选择对应的url,和参数。也可以保存
在这里插入图片描述

3.3、根据id查询一个(传参id)

在这里插入图片描述

3.4、新增添加(json格式)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值