记录 io.springfox 3.0.0 整合 spring boot 2.6.x 由于 springfox bug 引发问题

首先第一个问题就是不兼容:

解决方案:

java 复制代码
	@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) {
	                throw new IllegalStateException(e);
	            }
	        }
	    };
	}

其二增加配置:

spring:
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER  

而其中 springfox 中的一个 bug 就是,你在使用注解:paramType 由于没有使用指定的类型:path、query、body、header和form 导致启动解析的时候 enum 直接异常,导致整个项目所有注解都没有加载到系统内。解决办法:

  1. 把不正确的地方修改。

2.修改源码

源码修改如下:

java 复制代码
@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER)
@SuppressWarnings("deprecation")
public class OperationImplicitParametersReader implements OperationBuilderPlugin {
	private static final Logger LOGGER = LoggerFactory.getLogger(OperationImplicitParametersReader.class);
	
	private final DescriptionResolver descriptions;

	@Autowired
	public OperationImplicitParametersReader(DescriptionResolver descriptions) {
		this.descriptions = descriptions;
	}

	@Override
	public void apply(OperationContext context) {
		List<Compatibility<springfox.documentation.service.Parameter, RequestParameter>> parameters
		= readParameters(context);
		context.operationBuilder().parameters(parameters.stream()
				.map(Compatibility::getLegacy)
				.filter(Optional::isPresent)
				.map(Optional::get)
				.collect(Collectors.toList()));
		context.operationBuilder().requestParameters(parameters.stream()
				.map(Compatibility::getModern)
				.filter(Optional::isPresent)
				.map(Optional::get)
				.collect(Collectors.toList()));
	}

	@Override
	public boolean supports(DocumentationType delimiter) {
		return pluginDoesApply(delimiter);
	}

	private List<Compatibility<springfox.documentation.service.Parameter, RequestParameter>>
	readParameters(OperationContext context) {
		List<ApiImplicitParams> annotations = context.findAllAnnotations(ApiImplicitParams.class);

		List<Compatibility<springfox.documentation.service.Parameter, RequestParameter>> parameters = new ArrayList<>();
		if (!annotations.isEmpty()) {
			for (ApiImplicitParams annotation : annotations) {
				for (ApiImplicitParam param : annotation.value()) {
					Compatibility  compatibility = null;
					try {
						compatibility = OperationImplicitParameterReader.implicitParameter(descriptions, param);
					}catch(Exception e) {
						LOGGER.error(String.format("Unable to scan documentation Compatibility- ApiImplicitParam %s", param.value()), e);
						continue;
					}
					parameters.add(compatibility);
				}
			}
		}
		return parameters;
	}
}

关键修改:

Compatibility compatibility = null;

try {

compatibility = OperationImplicitParameterReader.implicitParameter(descriptions, param);

}catch(Exception e) {//在这个地方补货异常,并将异常处理,不往上抛

LOGGER.error(String.format("Unable to scan documentation Compatibility- ApiImplicitParam %s", param.value()), e);

continue;

}

parameters.add(compatibility);

相关推荐
ADFVBM36 分钟前
后端使用Spring Boot框架 + 前端VUE 实现滑动模块验证码
前端·vue.js·spring boot
m0_748239332 小时前
随手记录第十四话 -- 在 Spring Boot 3.2.3 中使用 springdoc-openapi-starter-webmvc-ui
spring boot·后端·ui
计算机学姐3 小时前
基于SpringBoot的校园求职招聘管理系统
java·前端·vue.js·spring boot·后端·mysql·intellij-idea
一起学习计算机4 小时前
29、基于springboot的网上购物商城系统研发
java·spring boot·后端
字母数字或汉字4 小时前
了解与配置 Git autocrlf 文本换行符处理
windows·git
Q_19284999065 小时前
基于Spring Boot的远程教育网站
java·spring boot·后端
龙少95436 小时前
【SpringBoot中怎么使用ElasticSearch】
spring boot·elasticsearch·jenkins
花花鱼6 小时前
VSCode中鼠标指针消失的原因及解决方法
ide·windows·vscode·编辑器
Q_19284999066 小时前
基于Spring Boot的店铺租赁平台的设计与实现
java·spring boot·后端