其他分享
首页 > 其他分享> > springboot集成swagger

springboot集成swagger

作者:互联网

springboot集成swagger

1.添加依赖

	<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>swagger-bootstrap-ui</artifactId>
        <version>1.9.6</version>
    </dependency>

2.添加配置

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
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;

/**
 * @Description swagger配置
 * WebMvcConfigurationSupport : 配置多个路由同时如果不重写addResourceHandlers方法,启动会报错
 * @Author dengfr
 * @Date 2022/1/23 下午9:35
 * @Version 1.0.0
 */
@EnableSwaggerBootstrapUI
@EnableSwagger2
@Configuration
public class Swagger2Config extends WebMvcConfigurationSupport {
	
	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.select()
				//为当前包路径,控制器类包
				.apis(RequestHandlerSelectors.basePackage("com.dd.xx.controller"))
				.paths(PathSelectors.any())
				.build();
	}
	//构建 api文档的详细信息函数
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				//页面标题
				.title("XX平台API接口文档")
				//创建人
				.contact(new Contact("dd","dd","xx@qq,com"))
				//版本号
				.version("1.0")
				//描述
				.description("系统API描述")
				.build();
	}
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("doc.html")
				.addResourceLocations("classpath:/META-INF/resources/");
		
		registry.addResourceHandler("/webjars/**")
				.addResourceLocations("classpath:/META-INF/resources/webjars/");
		
		registry.addResourceHandler("/**")
				.addResourceLocations("classpath:/static/");
		
		super.addResourceHandlers(registry);
	}
}

标签:集成,swagger,springboot,documentation,springframework,registry,org,import,springfo
来源: https://www.cnblogs.com/ODOD/p/15873063.html