<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>
package com.inschool.inschool.common.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* @author Andon
* 2023/10/27
*/
@EnableSwagger2
@Configuration
public class SwaggerConfig {
private static final String headerKey = "token"; //header参数的key
@Bean
public Docket createRestApi() {
// 添加header参数headerKey
ParameterBuilder parameterBuilder = new ParameterBuilder();
List<Parameter> parameterList = new ArrayList<>();
parameterBuilder.name(headerKey).description(headerKey)
.modelRef(new ModelRef("string")).parameterType("header")
.required(false).build();
parameterList.add(parameterBuilder.build());
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.inschool.inschool.controller"))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(parameterList)
.apiInfo(new ApiInfoBuilder()
.title("spring-boot-util")
.description("")
.version("v1.0")
.contact(new Contact("", "", ""))
.license("")
.licenseUrl("")
.build());
}
}
配置文件:
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
如果报错,加入
package com.inschool.inschool.common.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* @author Andon
* 2021/12/29
*/
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
/**
* 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}