当前位置:网站首页>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: 用于方法,接口错误所返回的信息
边栏推荐
- 695.最大岛屿面积
- Our company has used this set of general solutions for 7 years, and has opened up dozens of systems, a stable batch!
- wallys/600VX – 2×2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz QCA 9880
- Review the writing software with characteristics
- Stm32f407zgt6 uses SDIO mode to drive SD card
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set the alpha parameter in the point parameter to specify the transparency level of data points (points transparent
- 并行接口8255A
- MySQL 表的内连和外连
- A Generic Deep-Learning-Based Approach for Automated Surface Inspection-论文阅读笔记
- 来聊聊怎么做硬件兼容性检测,快速迁移到openEuler?
猜你喜欢
论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》
Analysis of KOA - onion model
60 divine vs Code plug-ins!!
Redis - ziplist compressed list
A Generic Deep-Learning-Based Approach for Automated Surface Inspection-論文閱讀筆記
1175. prime permutation
Boost study: boost log
服务器常用的一些硬件信息(不断更新)
MySQL 复合查询
A Generic Deep-Learning-Based Approach for Automated Surface Inspection-论文阅读笔记
随机推荐
Boost study: boost log
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)、使用labs函数为动画图添加动态时间标题
A High-Precision Positioning Approach for Catenary Support Components With Multiscale Difference阅读笔记
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ x_ The log10 function configures the value range of the X axis to be logarithmic coordinates
A Generic Deep-Learning-Based Approach for Automated Surface Inspection-论文阅读笔记
MySQL 表的内连和外连
Openmldb meetup No.4 meeting minutes
What is the function of LED backlight?
8253 counter introduction
21、wpf之绑定使用小记
Let's talk about how to do hardware compatibility testing and quickly migrate to openeuler?
It is said that with this, the boss opened the test overnight
TypeScript ReadonlyArray(只读数组类型) 详细介绍
【LeetCode】15、三数之和
wallys/IPQ8074a/2x(4×4 or 8×8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD
Constructor, class member, destructor call order
OpenMLDB Meetup No.4 会议纪要
Multiparty cardinality testing for threshold private set-2021: Interpretation
Object mapping - mapping Mapster
The sci-fi ideas in these movies have been realized by AI