第一步: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. CSS的相关知识——背景,超链接,列表,表格,奇偶选择器

    接着上一篇总结一些css的相关知识 ㈠背景 背景属性 1.background-color 背景颜色   rgb函数设置 2.background-image   背景图片  url(“logo.jp ...

  2. 51 Nod N^N的末位数字

    1004 n^n的末位数字  题目来源: Author Ignatius.L (Hdu 1061) 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 给 ...

  3. week6 作业

    week6 作业 1.每12小时备份并压缩/etc/目录至/backup目录中,保存文件名称格式为"etc-年-月-日-时-分.tar.gz" crontab -e */1 * * ...

  4. Unity3D_(API)Quaternion四元数中的Quaternion.LookRotation()

    四元数百度百科: 传送门 四元数官方文档: 传送门 欧拉旋转.四元数.矩阵旋转之间的差异: 传送门 四元数转换为欧拉角eulerAngles 官方文档: 传送门 欧拉角转换为四元数Euler 官方文档 ...

  5. 剑指offer-Q60 n个骰子的点数

    python版本代码 g_maxValue = 6 # 单个骰子最大的点数 def PrintProbability(number): ''' :param number: 骰子的个数 :return ...

  6. 翻译 | 上手 Webpack ? 这篇就够了!

    译者:小 boy (沪江前端开发工程师) 本文原创,转载请注明作者及出处. 原文地址:https://www.smashingmagazine.com/2017/02/a-detailed-intro ...

  7. mybatis逆向工程的text类型的一个小坑

    数据库如果配有text的数据类型的 mybatis生成逆向工程的时候会单独将text提取出来 ByExampleWithBLOBs 会生成上面后缀的查询和修改的语句 因此查询起来会产生没有必要的麻烦, ...

  8. Spring的jsp标签库

    1,主要有两个 一个用于渲染HTML表单标签, 这些标签会绑定model中的某个属性 另一个包换了一些工具类标签 2,将表单绑定到模型上 包含了14个标签,海报一个人为用户展现错误的标签,它会将错误信 ...

  9. go语言实现简易ftp客户端

          版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xiangxianghehe/article/details/78310249 Go语言实 ...

  10. 最近给几个CRM软件配套开发了Outlook插件,讲讲Outlook插件开发注意事项

    原始出处:www.cnblogs.com/Charltsing/p/OutlookAddinsTips.html联系QQ:564955427 从去年到现在,写了四五个Outlook插件,其中两个是给C ...