当前位置: 首页 > news >正文

RESTful风格接口+Swagger生成Web API文档

RESTful风格接口+Swagger生成Web API文档

文章目录

    • RESTful风格接口+Swagger生成Web API文档
      • 1.RESTful风格接口
        • RESTful简介
        • RESTful详细图示
        • 常见http状态码
        • springboot实现RESTful
          • RESTful springboot设计实例
          • demo
      • 2.Swagger生产Web API文档
        • Swagger简介
        • 使用Swagger
          • 1.加入依赖
          • 2.配置Swagger

1.RESTful风格接口

RESTful简介

在这里插入图片描述
也就是说,RESTful是一种接口标准
在这里插入图片描述
在这里插入图片描述

RESTful详细图示

在这里插入图片描述

常见http状态码

在这里插入图片描述

springboot实现RESTful

在这里插入图片描述
在这里插入图片描述

RESTful springboot设计实例

在这里插入图片描述

demo
package com.example.demo.controller;import com.example.demo.mapper.User;
import org.springframework.web.bind.annotation.*;@RestController
public class UserController {@GetMapping("/user/{id}")public String getUserById(@PathVariable int id) {return "根据ID获取用户";}@PostMapping("/user")public String save(User user) {return "添加用户";}@PutMapping("/user")public String update(User user) {return "更新用户";}@DeleteMapping("/user/{id}")public String deleteById(@PathVariable int id) {return "根据ID删除用户";}
}

2.Swagger生产Web API文档

Swagger简介

在这里插入图片描述

使用Swagger
1.加入依赖

在这里插入图片描述

<!-- Swagger 2.x 依赖 --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency>
2.配置Swagger

在这里插入图片描述
在这里插入图片描述

package com.example.demo.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration // 告诉Spring容器,这个类是一个配置类
@EnableSwagger2 // 启用Swagger2功能
public class Swagger2Config {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) // 这里缺少了方法调用的括号.select() // 构建API选择器.apis(RequestHandlerSelectors.basePackage("com")) // com包下所有API都交给Swagger2管理.paths(PathSelectors.any()) // 选择所有的路径.build();}// API文档页面显示信息private ApiInfo apiInfo() {return new ApiInfoBuilder().title("演示项目API") // 标题.description("学习Swagger2的演示项目") // 描述.build();}
}

在这里插入图片描述

http://www.lryc.cn/news/455034.html

相关文章:

  • 性能测试学习2:常见的性能测试策略(基准测试/负载测试/稳定性测试/压力测试/并发测试)
  • 【C++】—— 继承(上)
  • 【2024保研经验帖】东南大学计算机学院夏令营
  • dz论坛可可积分商城插件价值399元
  • python的extend和append
  • 贪心算法相关知识
  • 济南比较出名的人物颜廷利:全球最具影响力的思想家起名大师
  • 第100+27步 ChatGPT学习:概率校准 Temperature Scaling
  • Python知识点:如何应用Python工具,使用NLTK进行语言模型构建
  • 深入浅出MySQL
  • 【WRF工具】cmip6-to-wrfinterm工具概述:生成WRF中间文件
  • 大厂面试真题:阿里经典双重检测DCL对象半初始化问题
  • 20款奔驰CLS300升级原厂抬头显示HUD 23P智能辅助驾驶 触摸屏人机交互系统
  • GoogleNet原理与实战
  • MongoDB 数据库服务搭建(单机)
  • 基于springboot+小程序的智慧物业平台管理系统(物业1)
  • [SpringBoot] 苍穹外卖--面试题总结--上
  • [C#]使用onnxruntime部署yolov11-onnx实例分割模型
  • Polars的Config
  • 【面试官】 多态连环问
  • Vue 路由设置
  • 力扣110:判断二叉树是否为平衡二叉树
  • Chromium 中JavaScript Fetch API接口c++代码实现(一)
  • ARM(5)内存管理单元MMU
  • 文件上传漏洞原理
  • Web安全 - 安全防御工具和体系构建
  • 服务器数据恢复—raid磁盘故障导致数据库文件损坏的数据恢复案例
  • requests 中data=xxx、json=xxx、params=xxx 分别什么时候用
  • 毕设 大数据抖音短视频数据分析与可视化(源码)
  • 【SQL】深入理解SQL:从基础概念到常用命令