- maven依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter/3.0.0 --> <!-- 参考 https://stackoverflow.com/questions/62773219/suddenly-springfox-swagger-3-0-is-not-working-with-spring-webflux --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-ui --> <!-- http://localhost:8080/doc.html --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-ui</artifactId> <version>3.0.3</version> </dependency>
-
代码
@SpringBootApplication public class Application implements WebMvcConfigurer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public Docket createRestApi() { return new Docket(DocumentationType.OAS_30) .apiInfo(new ApiInfoBuilder() .description("面试") .title("标题") .version("版本 1.0.0") .build()) .groupName("测试组") .enable(true) .select() .apis(RequestHandlerSelectors.basePackage("com.demo")) .paths(PathSelectors.any()) .build(); } }
- 效果
@Bean
public Docket docket(Environment environment) {
// 设置了swagger的docket的环境
boolean flag = environment.acceptsProfiles(Profiles.of("dev", "test", "prod"));
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.groupName("g_name")
.enable(flag)
.select()
// 指定要扫描的包
.apis(RequestHandlerSelectors.any())
.build();
}
private ApiInfo apiInfo() {
Contact contact = new Contact("authorName", "www.baidu.com", "111111@qq.com");
return new ApiInfo("Api文档", "Api文档",
"1.0", "www.baidu.com",
contact, "Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
二:2023年04月22日15:13:26: 更新springboot 2.7.x swagger 界面不显示的问题
2.1:
springfox.documentation.swagger-ui.enabled=true knife4j.enable=true knife4j.production=false spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
2.2: code
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Preconditions;
import com.morris.learn.util.CastUtil;
import com.morris.learn.global.GlobalInterceptor;
import com.morris.learn.log.TraceIdInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.lang.NonNull;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
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.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Configuration
@RequiredArgsConstructor
@EnableOpenApi
@EnableKnife4j
public class WebConfig implements WebMvcConfigurer {
// private final GlobalInterceptor globalInterceptor;
// private final TraceIdInterceptor traceIdInterceptor;
//
// @Override
// public void addInterceptors(InterceptorRegistry registry) {
// registry.addInterceptor(traceIdInterceptor);
// registry.addInterceptor(globalInterceptor);
// }
@Override
public void addCorsMappings(CorsRegistry registry) {
// 覆盖所有请求
registry.addMapping("/**")
// 允许发送 Cookie
.allowCredentials(true)
// 放行哪些域名(必须用 patterns,否则 * 会和 allowCredentials 冲突)
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.exposedHeaders("*");
}
@Bean
public Docket docket() {
// 设置了swagger的docket的环境
return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).groupName("g_name").select()
// 指定要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.morris.learn")).paths(PathSelectors.any()).build()
;
}
private ApiInfo apiInfo() {
Contact contact = new Contact("authorName", "www.baidu.com", "111111@qq.com");
return new ApiInfo("Api文档", "Api文档", "1.0", "www.baidu.com",
contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<>());
}
@Bean
public static BeanPostProcessor springfoxHandler() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException {
if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
Preconditions.checkNotNull(field, "filed can not be null");
field.setAccessible(true);
try {
List<RequestMappingInfoHandlerMapping> mappings = CastUtil.cast(field.get(bean));
List<RequestMappingInfoHandlerMapping> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null).collect(Collectors.toList());
mappings.clear();
mappings.addAll(copy);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
};
}
}
2.3: util:
/**
* @author Fanhaibo 2023-04-22 13:38:10
*
*/
@SuppressWarnings("ALL")
public class CastUtil {
/**
* 方便强转
*/
public static <T> T cast(Object obj) {
return (T) obj;
}
}
代码参考链接:https://blog.csdn.net/qq_40663787/article/details/106333663