maven坐标
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.3</version>
</dependency>
配置类
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
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配置类
* @author czn
* @date 2021/12/24
*/
@SpringBootConfiguration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Config {
private static final Logger log = LoggerFactory.getLogger(Swagger2Config.class);
@Value("${swagger2.show}")
private Boolean show;
@Value("${swagger2.group-name}")
private String groupName;
@Value("${swagger2.base-package}")
private String basePackage;
@Value("${swagger2.title}")
private String title;
@Value("${swagger2.description}")
private String description;
@Value("${swagger2.terms-of-service-url}")
private String termsOfServiceUrl;
@Value("${swagger2.contact-name}")
private String contactName;
@Value("${swagger2.contact-url}")
private String contactUrl;
@Value("${swagger2.contact-email}")
private String contactEmail;
@Value("${swagger2.version}")
private String version;
@Bean
public Docket orderRestApi() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.enable(show)
.groupName(groupName)
.apiInfo(apiInfo())
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.any())
.build();
return docket;
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(title)
.description(description)
.termsOfServiceUrl(termsOfServiceUrl)
.contact(new Contact(contactName, contactUrl, contactEmail))
.version(version)
.build();
}
}
3 yml
swagger2:
show: true
group-name: ${spring.application.name}
base-package: com.czn
title: FileSystem服务接口文档
description: FileSystem服务接口文档
terms-of-service-url: http://127.0.0.1:${server.port}
contact-name: czn
contact-url: xxx
contact-email: xxx
version: V1.0
4 开放接口
@SpringBootApplication
@ConditionalOnClass(SpringfoxWebMvcConfiguration.class)
public class SwaggerBootstrapUiDemoApplication implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
5 访问地址 host+port+doc.html