SpringBoot2.7.15集成Springfox3.0

环境信息

操作系统:Windows 11

IDE:Eclipse 4.29

Java:JDK 1.8

集成步骤

Maven依赖

<dependency>

    <groupId>io.springfox</groupId>

    <artifactId>springfox-boot-starter</artifactId>

    <version>3.0.0</version>

</dependency>

 Springfox配置

创建配置类SpringfoxConfiguration.java


import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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 springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SpringfoxConfiguration {

	private final static Logger logger = LoggerFactory.getLogger(SpringfoxConfiguration.class);

	@Bean
	public Docket docketApi() {
		Docket docket = new Docket(DocumentationType.SWAGGER_2)//
		        .groupName("有关事项填报")//
		        .apiInfo(new ApiInfoBuilder()//
		                .title("有关事项填报平台API接口文档")//
		                .description("API接口文档")//
		                .version("0.0.1")//
		                .contact(new Contact("作者", null, "email@163.com"))//
		                .license("")//
		                .licenseUrl("")//
		                .termsOfServiceUrl("")//
//		                .extensions()
		                .build()//
				)//
		        .select() //
//		        .apis(RequestHandlerSelectors.any())//
		        .apis(RequestHandlerSelectors.basePackage("com"))//
		        .paths(PathSelectors.any()) //
		        .build() //
		;
		return docket;
	}

	/**
	 * 解决Springfox3.0启动报错问题
	 * 
	 * @return
	 */
	@Bean
	public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
		return new BeanPostProcessor() {

			@Override
			public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
				if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
					customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
				}
				return bean;
			}

			private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
				List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null).collect(Collectors.toList());
				mappings.clear();
				mappings.addAll(copy);
			}

			@SuppressWarnings("unchecked")
			private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
				try {
					Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
					field.setAccessible(true);
					return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
				} catch (IllegalArgumentException | IllegalAccessException e) {
					logger.warn("修改WebMvcRequestHandlerProvider的属性:handlerMappings出错,可能导致swagger不可用", e);
					throw new IllegalStateException(e);
				}
			}
		};
	}

}

启动系统的效果

注意访问地址:http://localhost/swagger-ui/index.html

解决“No operations defined in spec!”

 在application.yml配置文件中添加以下配置

spring:

    mvc:

        pathmatch:

            matching-strategy: ant_path_matcher

 添加配置后的结果

目前发现的遗留问题

无法识别ApiModel注解的模型类

一些交代

看了别人的博客加上自己实践得出。

看的博客内容不是很全。

写博客为了练习总结归纳。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值