当前位置:网站首页>Swagger2自动生成APi文档
Swagger2自动生成APi文档
2022-06-30 11:53:00 【Mr_Jin.】
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务的接口文档。
添加依赖:
<!--Swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!--Swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>进行创建SwaggerConfig配置
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder().title("微信支付案例接口文档").build());
}
}
代码中进行注解使用:
在接口文件中增加对应注解。代码如下,由于我们第二步选择扫描接口的方式是在类前添加@Api;@ApiOperation用于注明接口,value是接口的解释;@ApiParam注解函数里面的参数,name一般与参数名一致,value是解释,required是是否参数必须。
import com.jt.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@Api("商品管理")
@RequestMapping("/api/product")
@RestController
public class ProductController {
@ApiOperation("测试接口")
@RequestMapping(value = "/test",method = RequestMethod.GET)
public R test(@ApiParam(name="name",value="对话人",required=true) String name){
return R.ok().data("666","测试").data("时间",new Date());
}
}
注意:在接口中使用了@RequestMapping时,必须指定接口的形式,是get还是post等。否则Swagger会把所有类型都创建。
上面操作都完成后,在浏览器中输入网址:http://localhost:8080/swagger-ui.html

swagger api注解
这一部分除了,下面列出的注解外,还包括上面所介绍的RequestHandlerSelectors和PathSelectors的几种方法及含义。
@Api: 用于类,标识这个类是swagger的资源
@ApiIgnore: 用于类,忽略该 Controller,指不对当前类做扫描
@ApiOperation: 用于方法,描述 Controller类中的 method接口
@ApiParam: 用于参数,单个参数描述,与 @ApiImplicitParam不同的是,他是写在参数左侧的。如( @ApiParam(name="username",value="用户名")Stringusername)
@ApiModel: 用于类,表示对类进行说明,用于参数用实体类接收
@ApiProperty:用于方法,字段,表示对model属性的说明或者数据操作更改
@ApiImplicitParam: 用于方法,表示单独的请求参数
@ApiImplicitParams: 用于方法,包含多个 @ApiImplicitParam
@ApiResponse: 用于方法,描述单个出参信息
@ApiResponses: 用于方法,包含多个@ApiResponse
@ApiError: 用于方法,接口错误所返回的信息
边栏推荐
- MATLAB中polarplot函数使用
- 【BUG解决】fiftyone报AttributeError: module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘错误解决方法
- Getting started with the go language is simple: go handles XML files
- 1175. 质数排列 : 乘法原理运用题
- DMA控制器8237A
- Installing onnx is very slow. Use Tsinghua image
- DMA controller 8237a
- Cache avalanche and cache penetration solutions
- 并行接口8255A
- R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题
猜你喜欢

线下门店为什么要做新零售?

In depth analysis of Apache bookkeeper series: Part 4 - back pressure

The sci-fi ideas in these movies have been realized by AI

Limited time appointment | Apache pulsar Chinese developer and user group meeting in June

A high precision positioning approach for category support components with multiscale difference reading notes

YOLOv5导出onnx遇到的坑

Who still remembers "classmate Zhang"?

lvgl 小部件样式篇

c# 怎样能写个sql的解析器

AUTOCAD——LEN命令
随机推荐
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ color_ viridis_ D function specifies the color scheme of data points
聊聊怎么做硬件兼容性检测,快速迁移到openEuler?
Shell first command result is transferred to the second command delete
[pattern recognition]
Stm32f407zgt6 uses SDIO mode to drive SD card
Global Capital Market 101:国内高净值人群最好的投资标的之一BREIT
DMA controller 8237a
Constructor, class member, destructor call order
60 divine vs Code plug-ins!!
Flutter 从零开始 007 输入框
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ The size function configures the measurement adjustment range of the size of the data point
Object mapping - mapping Mapster
安装onnx很慢,使用清华镜像
并行接口8255A
200. 岛屿数量
Conference Preview - Huawei 2012 lab global software technology summit - European session
21、wpf之绑定使用小记
MySQL索引和优化的理解学习
Lucene full text search toolkit learning notes summary
Multiparty Cardinality Testing for Threshold Private Set-2021:解读