当前位置:网站首页>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: 用于方法,接口错误所返回的信息
边栏推荐
- Paper interpretation (AGC) attributed graph clustering via adaptive graph revolution
- time 函数和 clock_gettime()函数的区别
- Use of redis in projects
- HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴
- AUTOCAD——LEN命令
- Conference Preview - Huawei 2012 lab global software technology summit - European session
- Speech signal processing - Fundamentals (V): Fourier transform
- He was the first hero of Shanghai's two major industries, but died silently in regret
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and_ Set show in the point parameter_ The legend parameter is false, and the legend information is not displayed
- R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的size参数指定数据点的大小(point size)
猜你喜欢
随机推荐
695. maximum island area
STM32F407ZGT6使用SDIO方式驱动SD卡
学习redis实现分布式锁—–自己的一个理解
治数如治水,数据治理和数据创新难在哪?
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
Installing onnx is very slow. Use Tsinghua image
网络营销之四大误解
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的size参数指定数据点的大小(point size)
wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
R language ggplot2 visual Facet: gganimate package is based on Transition_ The time function creates a dynamic scatter graph animation (GIF) and uses the labs function to add a dynamic time title to t
A quietly rising domestic software, low-key and powerful!
Multiparty Cardinality Testing for Threshold Private Set-2021:解读
STM32 移植 RT-Thread 标准版的 FinSH 组件
R language ggplot2 visualization: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
Redis - ziplist compressed list
Statistics on the number of closed Islands
lvgl 小部件样式篇
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
3D视觉检测在生产流水的应用有哪些
Boost study: boost log









