第一步: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 。遇到这个问题很有可能就是被拦截了。

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

开发完成的springboot项目扩展 swagger的更多相关文章

  1. SpringBoot项目集成swagger报NumberFormatException: For input string: ""

    java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.f ...

  2. SpringBoot项目集成Swagger启动报错: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is

    使用的Swagger版本是2.9.2.knife4j版本是2.0.4. SpringBoot 版本是2.6.2将SpringBoot版本回退到2.5.6就可以正常启动

  3. eclipse 开发 spring 、 springboot项目调试时一直跳转到 SilentExitExceptionHandler.exitCurrentThread 方法

    不想让 eclipse 调试时一直跳转到该方法 解决方法: Eclipse->[Preferences]->[Java]->[Debug]:去掉[Suspend execution ...

  4. springboot+mybatis-puls利用swagger构建api文档

    项目开发常采用前后端分离的方式.前后端通过API进行交互,在Swagger UI中,前后端人员能够直观预览并且测试API,方便前后端人员同步开发. 在SpringBoot中集成swagger,步骤如下 ...

  5. 【Docker】Maven打包SpringBoot项目成Docker镜像并上传到Harbor仓库(Eclipse、STS、IDEA、Maven通用)

    写在前面 最近,在研究如何使用Maven将SpringBoot项目打包成Docker镜像并发布到Harbor仓库,网上翻阅了很多博客和资料,发现大部分都是在复制粘贴别人的东西,没有经过实践的检验,根本 ...

  6. 从零开始的SpringBoot项目 ( 五 ) 整合 Swagger 实现在线API文档的功能

    综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...

  7. JAVA中自定义扩展Swagger的能力,自动生成参数取值含义说明,提升开发效率

    大家好,又见面了. 在JAVA做前后端分离的项目开发的时候,服务端需要提供接口文档供周边人员做接口的对接指导.越来越多的项目都在尝试使用一些基于代码自动生成接口文档的工具来替代由开发人员手动编写接口文 ...

  8. springboot项目--传入参数校验-----SpringBoot开发详解(五)--Controller接收参数以及参数校验----https://blog.csdn.net/qq_31001665/article/details/71075743

    https://blog.csdn.net/qq_31001665/article/details/71075743 springboot项目--传入参数校验-----SpringBoot开发详解(五 ...

  9. Kotlin开发springboot项目(三)

    Kotlin开发springboot项目(三) 在线工具 https://www.sojson.com IDEA中Kotlin生成可执行文件1,项目使用Gradle构建2,在model的build.g ...

随机推荐

  1. luogu小金明qwq x

    1.P1060 开心的金明 题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算, ...

  2. VMware Guest customization fails on Linux

    1.1  症状现象 登录Guest OS,在/var/log/vmware-imc/toolsDeployPkg.log文件中,您会看到以下条目: Customization command fail ...

  3. Win10上安装Awvs 12原版程序和完美破解补丁详细步骤

    环境: Win10 Awvs12安装包 链接:https://pan.baidu.com/s/1FIwYHIEKfLf4XAyeXfhVnA 提取码:6sa8 复制这段内容后打开百度网盘手机App,操 ...

  4. java知识查漏补缺

    一.重写(override)和重载(overload)的区别 二者除了名字相似,其实没什么联系 范围不同:重载发生在同一个类的不同方法之间.重写发生在父类和子类自荐. 前提: 重载要求:方法名相同,参 ...

  5. HBase2.0新特性解析

    作者 | 个推大数据运维工程师 行者 升级背景 个推作为专业的数据智能服务商,在业务开展过程中存在海量的数据存储与查询的需求,为此个推选用了高可靠.高性能.面向列.可伸缩的分布式数据存储系统--HBa ...

  6. 前端面试题总结——HTML(持续更新中)

    前端面试题总结--HTML(持续更新中) 1.什么是HTML? HTML:HyperText Markup Language超文本标记语言 2.XHTML和HTML有什么区别 HTML是一种基本的WE ...

  7. 上传本地文件到linux

    Linux 命令行上传文件 Linux服务器,硬盘坏了,更换硬盘后,重装了好像是Foreda v22的操作系统,我在/var/www/html/目录下新建了我的个人文件夹 kma,然后我在此kma文件 ...

  8. 第七周总结&实验报告5

    这一周的课程内容比较难,而且比较不容易理解,所有学习的很吃力,现在接触的知识越来越多,也越来越难了,还是要多对照书本来进行学习! 这周主要学的有: 一.抽象类 1.Java中可以创建一种类专门用来当作 ...

  9. spark MLlib 概念 1:相关系数( PPMCC or PCC or Pearson's r皮尔森相关系数) and Spearman's correlation(史匹曼等级相关系数)

    皮尔森相关系数定义: 协方差与标准差乘积的商. Pearson's correlation coefficient when applied to a population is commonly r ...

  10. koa2中间键原理

    一.koa2 const http = require('http'); const compose = require('./compose'); class Koa { constructor() ...