栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

完美解决springboot2.6+和swagger的冲突问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

完美解决springboot2.6+和swagger的冲突问题

不罗嗦直接上代码!可直接复制代码到自己项目即可!

  1. 用BeanDefinitionRegistryPostProcessor替换Bean: WebMvcRequestHandlerProvider
    public class CompatiblePathPatternParser implements BeanDefinitionRegistryPostProcessor, EnvironmentAware {
        static final String BEAN_NAME = "webMvcRequestHandlerProvider";

        private Environment environment;

        @Override
        public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
            WebMvcProperties.MatchingStrategy strategy = environment.getProperty("spring.mvc.pathmatch.matching-strategy", WebMvcProperties.MatchingStrategy.class);
            if(registry.containsBeanDefinition(BEAN_NAME) && strategy != WebMvcProperties.MatchingStrategy.ANT_PATH_MATCHER){
                registry.removeBeanDefinition(BEAN_NAME);
                registry.registerBeanDefinition(BEAN_NAME,new RootBeanDefinition(WebMvcRequestHandlerProvider.class));
            }
        }

        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        }

        @Override
        public void setEnvironment(Environment environment) {
            this.environment = environment;
        }

    }
  1. WebMvcPatternsRequestConditionWrapper
public class WebMvcPatternsRequestConditionWrapper implements springfox.documentation.spring.wrapper.PatternsRequestCondition {

    private final String contextPath;
    private final PathPatternsRequestCondition condition;

    public WebMvcPatternsRequestConditionWrapper(String contextPath, PathPatternsRequestCondition condition) {
        this.contextPath = contextPath;
        this.condition = condition;
    }

    @Override
    public springfox.documentation.spring.wrapper.PatternsRequestCondition combine(
            springfox.documentation.spring.wrapper.PatternsRequestCondition other) {
        if (other instanceof WebMvcPatternsRequestConditionWrapper && !this.equals(other)) {
            return new WebMvcPatternsRequestConditionWrapper(
                    contextPath,
                    condition.combine(((WebMvcPatternsRequestConditionWrapper) other).condition));
        }
        return this;
    }

    @Override
    public Set getPatterns() {
        return this.condition.getPatterns().stream()
                .map(p -> String.format("%s/%s", maybeChompTrailingSlash(contextPath),  maybeChompLeadingSlash(p.getPatternString())))
                .collect(Collectors.toSet());
    }


    @Override
    public boolean equals(Object o) {
        if (o instanceof WebMvcPatternsRequestConditionWrapper) {
            return this.condition.equals(((WebMvcPatternsRequestConditionWrapper) o).condition);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.condition.hashCode();
    }


    @Override
    public String toString() {
        return this.condition.toString();
    }
}
  1. WebMvcRequestHandler
public class WebMvcRequestHandler implements RequestHandler {
    private final String contextPath;
    private final HandlerMethodResolver methodResolver;
    private final RequestMappingInfo requestMapping;
    private final HandlerMethod handlerMethod;

    public WebMvcRequestHandler(
            String contextPath,
            HandlerMethodResolver methodResolver,
            RequestMappingInfo requestMapping,
            HandlerMethod handlerMethod) {
        this.contextPath = contextPath;
        this.methodResolver = methodResolver;
        this.requestMapping = requestMapping;
        this.handlerMethod = handlerMethod;
    }

    @Override
    public HandlerMethod getHandlerMethod() {
        return handlerMethod;
    }

    @Override
    public RequestHandler combine(RequestHandler other) {
        return this;
    }

    @Override
    public Class declaringClass() {
        return handlerMethod.getBeanType();
    }

    @Override
    public boolean isAnnotatedWith(Class annotation) {
        return null != AnnotationUtils.findAnnotation(handlerMethod.getMethod(), annotation);
    }

    @Override
    public PatternsRequestCondition getPatternsCondition() {
        return new WebMvcPatternsRequestConditionWrapper(contextPath,requestMapping.getPathPatternsCondition());
    }

    @Override
    public String groupName() {
        return ControllerNamingUtils.controllerNameAsGroup(handlerMethod);
    }

    @Override
    public String getName() {
        return handlerMethod.getMethod().getName();
    }

    @Override
    public Set supportedMethods() {
        return requestMapping.getMethodsCondition().getMethods();
    }

    @Override
    public Set produces() {
        return requestMapping.getProducesCondition().getProducibleMediaTypes();
    }

    @Override
    public Set consumes() {
        return requestMapping.getConsumesCondition().getConsumableMediaTypes();
    }

    @Override
    public Set> headers() {
        return WebMvcNameValueExpressionWrapper.from(requestMapping.getHeadersCondition().getExpressions());
    }

    @Override
    public Set> params() {
        return WebMvcNameValueExpressionWrapper.from(requestMapping.getParamsCondition().getExpressions());
    }

    @Override
    public  Optional findAnnotation(Class annotation) {
        return ofNullable(AnnotationUtils.findAnnotation(handlerMethod.getMethod(), annotation));
    }

    @Override
    public RequestHandlerKey key() {
        return new RequestHandlerKey(
                requestMapping.getPathPatternsCondition().getDirectPaths(),
                requestMapping.getMethodsCondition().getMethods(),
                requestMapping.getConsumesCondition().getConsumableMediaTypes(),
                requestMapping.getProducesCondition().getProducibleMediaTypes());
    }

    @Override
    public springfox.documentation.spring.wrapper.RequestMappingInfo getRequestMapping() {
        return new WebMvcRequestMappingInfoWrapper(requestMapping);
    }

    @Override
    public List getParameters() {
        return methodResolver.methodParameters(handlerMethod);
    }

    @Override
    public ResolvedType getReturnType() {
        return methodResolver.methodReturnType(handlerMethod);
    }

    @Override
    public  Optional findControllerAnnotation(Class annotation) {
        return ofNullable(AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), annotation));
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", WebMvcRequestHandler.class.getSimpleName() + "{", "}")
                .add("requestMapping=" + requestMapping)
                .add("handlerMethod=" + handlerMethod)
                .add("key=" + key())
                .toString();
    }
}
  1. WebMvcRequestHandlerProvider
public class WebMvcRequestHandlerProvider implements RequestHandlerProvider {
    private final List handlerMappings;
    private final HandlerMethodResolver methodResolver;
    private final String contextPath;

    public WebMvcRequestHandlerProvider(Optional servletContext,HandlerMethodResolver methodResolver,List handlerMappings) {
        this.handlerMappings = handlerMappings;
        this.methodResolver = methodResolver;
        this.contextPath = servletContext.map(ServletContext::getContextPath).orElse(ROOT);
    }

    @Override
    public List requestHandlers() {
        return nullToEmptyList(handlerMappings).stream()
                .filter(requestMappingInfoHandlerMapping ->
                        !("org.springframework.integration.http.inbound.IntegrationRequestMappingHandlerMapping"
                                .equals(requestMappingInfoHandlerMapping.getClass()
                                        .getName())))
                .map(toMappingEntries())
                .flatMap((entries -> StreamSupport.stream(entries.spliterator(), false)))
                .map(toRequestHandler())
                .sorted(byPatternsCondition())
                .collect(toList());
    }

    private Function>> toMappingEntries() {
        return input -> input.getHandlerMethods()
                .entrySet();
    }

    private Function, RequestHandler> toRequestHandler() {
        return input -> new WebMvcRequestHandler(
                contextPath,
                methodResolver,
                input.getKey(),
                input.getValue());
    }
}
暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1016657.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号