首先第一个问题就是不兼容:
解决方案:
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 直接异常,导致整个项目所有注解都没有加载到系统内。解决办法:
- 把不正确的地方修改。
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);