引入jar包
参考swagger官网http://springfox.github.io/springfox/docs/current/#quick-start-guides
<!--swagger引入-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
swagger配置
package com.graduate.partybuilding.config;
import com.fasterxml.classmate.TypeResolver;
import com.graduate.partybuilding.auth.util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.*;
import java.time.LocalDate;
import java.util.List;
import static java.util.Collections.singletonList;
import static springfox.documentation.schema.AlternateTypeRules.newRule;
//配置类注解
@Configuration
@EnableOpenApi
public class SwaggerConfig {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
//扫描该包下面的API注解
.apis(RequestHandlerSelectors.basePackage("com.graduate.partybuilding"))
.paths(PathSelectors.any())
.build()
.pathMapping("/")
.useDefaultResponseMessages(false)
.securitySchemes(singletonList(apiKey()))
.enableUrlTemplating(true);
}
/**
* token 认证
* @return
*/
private ApiKey apiKey() {
return new ApiKey(JwtTokenUtil.TOKEN_HEADER, JwtTokenUtil.TOKEN_HEADER, "header");
}
@Bean
UiConfiguration uiConfig() {
return UiConfigurationBuilder.builder()
.deepLinking(true)
.displayOperationId(false)
.defaultModelsExpandDepth(1)
.defaultModelExpandDepth(1)
.defaultModelRendering(ModelRendering.EXAMPLE)
.displayRequestDuration(false)
.docExpansion(DocExpansion.NONE)
.filter(false)
.maxDisplayedTags(null)
.operationsSorter(OperationsSorter.ALPHA)
.showExtensions(false)
.showCommonExtensions(false)
.tagsSorter(TagsSorter.ALPHA)
.supportedSubmitMethods(UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS)
.validatorUrl(null)
.build();
}
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui/index.html
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//接口管理文档首页显示
.title("title")
//API文档的描述
.description("描述")
.contact(new Contact("作者姓名","IP address","email"))
//网站url
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}
开放security的拦截
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers(
"/v3/api-docs",
"/swagger-resources/**",
"/swagger-ui/**",
"/webjars/**");
}
}
开放mvc对静态资源的配置
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
/*
* 配置静态资源,避免静态资源请求被拦截
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
super.addResourceHandlers(registry);
}