zoukankan      html  css  js  c++  java
  • Springboot 集成Swagger

    Springboot版本 2.1.14.RELEASE
    Swagger版本 2.9.2

    1. 引入jar pom.xml中加入

    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger2</artifactId>
          <version>2.9.2</version>
    </dependency>
    <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger-ui</artifactId>
          <version>2.9.2</version>
    </dependency>
    

    2. 加入配置

    public class WebConfiguration implements WebMvcConfigurer {
    
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
            "classpath:/static/");
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations(
            "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
            "classpath:/META-INF/resources/webjars/");
      }
    }
    

    3. 如果发现配置了之后仍然无法访问,可以跟踪下源代码 找到正确答案

    通过 addResourceHandlers找到父类 org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration#addResourceHandlers
    再向上一级 org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#resourceHandlerMapping

        @Bean
        @Nullable
        public HandlerMapping resourceHandlerMapping() {
            Assert.state(this.applicationContext != null, "No ApplicationContext set");
            Assert.state(this.servletContext != null, "No ServletContext set");
    
            ResourceHandlerRegistry registry = new ResourceHandlerRegistry(this.applicationContext,
                this.servletContext, mvcContentNegotiationManager(), mvcUrlPathHelper());
            **addResourceHandlers(registry);**
    
            AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
            if (handlerMapping == null) {
                return null;
            }
            handlerMapping.setPathMatcher(mvcPathMatcher());
            handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
            handlerMapping.setInterceptors(getInterceptors());
            handlerMapping.setCorsConfigurations(getCorsConfigurations());
            return handlerMapping;
        }      
    

    关键一行代码 addResourceHandlers(registry);

    WebMvcConfigurationSupport 有一个空的实现方法
    /**
      * Override this method to add resource handlers for serving static resources.
      * @see ResourceHandlerRegistry
    */
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {      
    }
    

    有人提示 重写该方法,我试了下没有作用。
    别人的答案永远只是必然的答案
    不过你可以试试,对不对全看运气。

    跟踪源码找答案,进步中。。。

  • 相关阅读:
    SQL SERVER 表分区测试备忘
    理解信息系统中的流程与工序
    获取最后插入的id另外方法
    asp.net 项目在 IE 11 下出现 “__doPostBack”未定义 的解决办法
    定制应用Repeater 、ListView的模版
    JavaScript学习笔记——简单无缝循环滚动展示图片的实现
    JavaScript学习笔记——JS中的变量复制、参数传递和作用域链
    CSS学习笔记——定位position属性的学习
    CSS学习笔记——CSS中定位的浮动float
    CSS学习笔记——盒模型,块级元素和行内元素的区别和特性
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/13186722.html
Copyright © 2011-2022 走看看