开发完成的springboot项目扩展 swagger

第一步:pom.xml 引入 swagger 配置

<swagger.version>2.9.2</swagger.version>
<!--swagger start-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger.version}</version>
</dependency>

第二步:编写 swagger配置

package com.guoll.modules.config.swagger;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger配置
 * @author 开发者
* @Configuration 表明是一个 配置类
* @EnableSwagger2 开启 swagger */ @Configuration @EnableSwagger2 public class swaggerConfig { /** * 构建 swagger * @return */ @Bean public Docket createRestApi(){ /*return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包路径 .apis(RequestHandlerSelectors.basePackage("com.guoll.modules.config.swagger2")) .paths(PathSelectors.any()) .build();*/ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //这里采用包含注解的方式来确定要显示的接口 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } /** * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个 * @return */ private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 .title("Spring Boot 测试使用 Swagger2 构建RESTful API") //创建人 .contact(new Contact("MarryFeng", "http://www.baidu.com", "")) //版本号 .version("1.0") //描述 .description("API 描述") .build(); } }

第三步:映射 swagger 资源配置

package com.guoll.modules.config;

import com.guoll.modules.interceptor.FileInterceptor;
import com.guoll.modules.interceptor.TokenInterceptor;
import com.guoll.modules.listener.LoginSessionLister;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.*;

import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 核心配置
 * 已经完成的项目 配置肯定不是只有这么一个, 但是为了安全,只展示 有关 
 * swagger的
 */
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {

    /**
     * 重写 addResourceHandlers
     * 进行资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        // 映射 swagger 资源
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);

    }


}

第四步:启动类上扫描 swagger 包 @ComponentScan(basePackages = {"com.guoll.modules.config.swagger.*"})

第五步:本项目采用 shiro 作为权限管理,所以需要对 swagger 一些路径进行放行

第六步:过滤器放行

问题处理

org.springframework.context.ApplicationContextException 异常: 
解决原因 swagger 需要用到 com.google.guava ,而我的 guava 版本太低了 改成了 目前版本比较高的 28.0-jre
运行 OK
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; 
nested exception is java.lang.NoSuchMethodError: com.google.common.collect.Multimaps.asMap(Lcom/google/common/collect/ListMultimap;)Ljava/util/Map;

至于 swagger-ui.html 404 和 swagger-ui.html 弹窗

我也遇到过,并且心很塞。但是我的问题 就是 上面说的 第五步,和第六步没做到位。 swagger-ui.html 被拦截。

遇到此问题 首先 打开 浏览器的控制台,看看是否出现 swagger 有关的 404 和 302 。遇到这个问题很有可能就是被拦截了。

这里之所以 写这么少,是因为这些问题我遇到了,也解决了,并且是按照上面思路解决的。

原文地址:https://www.cnblogs.com/zhangzhonghui/p/11543337.html