swagger自动生成API文档
Posted 全力付出
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swagger自动生成API文档相关的知识,希望对你有一定的参考价值。
swagger自动生成API文档
一、pom.xml配置
<!--swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
二、spring boot配置
package cn.pomelo.web.config;
import org.springframework.beans.factory.annotation.Value;
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;
/**
* swagger自动生成API文档 (http://127.0.0.1:8080/swagger-ui.html)
*/
@EnableSwagger2
@Configuration
public class SwaggerConfig
//是否开启swagger,正式环境一般是需要关闭的,可根据springboot的多环境配置进行设置
@Value(value = "$swagger.enabled")
Boolean swaggerEnabled;
@Bean
public Docket createRestApi()
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
// 是否开启
.enable(swaggerEnabled).select()
// 扫描的路径包
.apis(RequestHandlerSelectors.basePackage("cn.pomelo.web.controller"))
// 指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any()).build().pathMapping("/");
private ApiInfo apiInfo()
return new ApiInfoBuilder()
.title("SpringBoot-Swagger2集成和使用-demo示例")
.description("spring boot swagger restful api document")
// 作者信息
.contact(new Contact("yong.zheng", "https://blog.csdn.net/zhengyong15984285623?viewmode=contents", "zyongjava@163.com"))
.version("1.0.0")
.build();
三、swagger-ui.html配置
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* MVC配置
*
*/
@Configuration
public class CustomMvcConfig extends WebMvcConfigurerAdapter
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
super.addResourceHandlers(registry);
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
四、注解说明
常用到的注解:
@Api
@ApiModel
@ApiModelProperty
@ApiOperation
@ApiParam
@ApiResponse
@ApiResponses
@ResponseHeader
地址:https://www.cnblogs.com/hyl8218/p/8520994.html
以上是关于swagger自动生成API文档的主要内容,如果未能解决你的问题,请参考以下文章