引言
最近在后台开发的时候,使用swagger2进行前后台接口文档的声明。由此遇见的一些问题,写下来给自己复习。
参考:
https://blog.youkuaiyun.com/xupeng874395012/article/details/68946676
正文
在进行整合swagger2的时候,首先引入swagger2的jar,由于我使用的是springboot,所以以springboot为例。
<!--springboot 集成 swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
引入swagger2的jar包之后,我们需要配置一个swagger2的配置类,来声明一些swagger2的配置信息
这样的话,swagger2就已经配置完毕了。接下来你只需要在你的接口上配置你想要显示的信息即可。
@Configuration //表示是配置类,要被加载
@EnableSwagger2 //swagger的配置
public class Swagger2 {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //添加ApiOperiation注解的被扫描
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
Contact contact = new Contact("xx", "www.baidu.com", "xxx@126.com");
return new ApiInfoBuilder()
.title("cc").contact(contact).description("接口文档").license("Apache License Version 2.0")
.version("v1.0").build();
}
}
本文详细介绍如何在SpringBoot项目中集成Swagger2,包括引入依赖、配置类编写及接口注解使用等内容,帮助开发者快速掌握Swagger2的配置与应用。
611

被折叠的 条评论
为什么被折叠?



