Springboot整合SwaggerAPI文档接口
程序员文章站
2022-07-15 10:24:40
...
Springboot整合SwaggerAPI文档接口
配置project.pom
maven
<!-- Swagger2 接口文档 2.9.2 -->
<dependency>
<groupId>io.github.wilson-he</groupId>
<artifactId>swagger2-spring-boot-starter</artifactId>
<version>LATEST</version>
</dependency>
配置application.yml
swagger:
enabled: true
docket:
api-info:
description: API文档
title: 梓晨-API
version: 1.0.1
contact:
name: zc
email: [email protected]
url: www.baidu.com
license: 禁止未经主人同意私自传播
license-url: https://www.baidu.com
group-name: Demo模块
base-package: com.zc.demo
dockets:
docket-redis:
group-name: Redis模块
api-info:
description: Redis-API
title: zc-Redis
version: 1.0.2
contact:
name: zcRedis
email: [email protected]
url: https://www.baidu.com
license: 禁止未经主人同意私自传播!
license-url: https://www.baidu.com
base-package: com.zc.redis
如果不用application.yml配置,也可使用实体类自行配置
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 访问地址例如:http://127.0.0.1:9091/zc/swagger-ui.html
*
* 可用注解:
*
* 1.swagger的注解主要在包‘io.swagger.annotations’下,除了以上描述的注解外,还有@ApiImplicitParam,@ApiImplicitParams,@ApiKeyAuthDefinition,@ApiResponse,@ApiResponses,
* @Authorization,@AuthorizationScope,@BasicAuthDefinition,@Contact,@Example,@ExampleProperty,
* @Extension,@ExtensionProperty,@ExternalDocs,@Info,@License,@OAuth2Definition,@ResponseHeader,
* @Scope,@SecurityDefinition,@SwaggerDefinition,@Tag
*
* 2.除了swagger自己定义的注解外,还有部分校验注解,注解所在包‘javax.validation.constraints’下,像@Max,@Min,@Size这些也是可用的。
*
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
/**
* UI页面显示信息
*/
private final String SWAGGER2_API_BASEPACKAGE = "com.zc";
private final String SWAGGER2_API_TITLE = "梓晨-API";
private final String SWAGGER2_API_DESCRIPTION = "zc-API文档";
private final String SWAGGER2_API_CONTACT_NAME = "梓晨";
private final String SWAGGER2_API_VERSION = "1.0";
/**
* createRestApi
*
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER2_API_BASEPACKAGE))
.paths(PathSelectors.any())
.build();
}
/**
* apiInfo
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(SWAGGER2_API_TITLE)
.description(SWAGGER2_API_DESCRIPTION)
.contact(new Contact(SWAGGER2_API_CONTACT_NAME,"https://www.baidu.com","[email protected]"))
.version(SWAGGER2_API_VERSION)
.license("123456")
.licenseUrl("www.baidu.com")
.build();
}
}
项目启动后使用项目根访问路径+/swagger-ui.html
例如http://127.0.0.1:8080/demo/swagger-ui.html
相关注解请参考:https://blog.csdn.net/hulei19900322/article/details/78107516
相关application.yml配置请参考:https://blog.csdn.net/z28126308/article/details/84187221
目前contact=>email项,在页面的a标签内无法实现跳转,不知为何,如有大神,请多指教。
推荐阅读
-
SpringBoot(六) SpringBoot整合Swagger2(自动化生成接口文档)
-
Springboot集成接口文档swagger,使用,测试
-
SpringBoot整合mybatis实现后端接口
-
SpringBoot整合Forest实现调用第三方接口
-
Springboot整合SwaggerAPI文档接口
-
SpringBoot 使用 swagger2 构建在线接口文档
-
springboot中使用swagger2构建restful接口文档
-
SpringBoot添加swagger2接口文档并添加全局Authorization参数
-
Java开发SpringBoot集成接口文档实现示例
-
java集成开发SpringBoot生成接口文档示例实现