第一步: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*/@Beanpublic 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 资源配置

packagecom.guoll.modules.config;importcom.guoll.modules.interceptor.FileInterceptor;importcom.guoll.modules.interceptor.TokenInterceptor;importcom.guoll.modules.listener.LoginSessionLister;importorg.apache.tomcat.util.descriptor.web.SecurityCollection;importorg.apache.tomcat.util.descriptor.web.SecurityConstraint;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;importorg.springframework.boot.web.server.ConfigurableWebServerFactory;importorg.springframework.boot.web.server.ErrorPage;importorg.springframework.boot.web.server.WebServerFactoryCustomizer;importorg.springframework.boot.web.servlet.MultipartConfigFactory;importorg.springframework.boot.web.servlet.ServletListenerRegistrationBean;importorg.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.http.HttpStatus;importorg.springframework.web.context.request.RequestContextHolder;importorg.springframework.web.context.request.RequestContextListener;importorg.springframework.web.context.request.ServletRequestAttributes;importorg.springframework.web.servlet.config.annotation.PathMatchConfigurer;import org.springframework.web.servlet.config.annotation.*;importjavax.servlet.MultipartConfigElement;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpSession;/*** 核心配置* 已经完成的项目 配置肯定不是只有这么一个, 但是为了安全,只展示 有关 * swagger的*/@Configurationpublic class MyWebMvcConfigurer implementsWebMvcConfigurer {/*** 重写 addResourceHandlers* 进行资源映射*@paramregistry*/@Overridepublic voidaddResourceHandlers(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

开发完成的springboot项目扩展 swagger相关推荐

  1. Springboot项目集成Swagger

    官网:https://swagger.io 一.准备工作 1). 导入knife4j的maven坐标 <dependency><groupId>com.github.xiaoy ...

  2. springboot项目使用swagger时拦截器需要放开哪些URL

    1.如果不设置会导致swagger主页无法访问 2.以下url不需要拦截 /swagger-resources/*, /swagger-ui.html,/v2/api-docs, /webjars/s ...

  3. 如何在IDE部署springboot项目(有swagger和无swagger都是一样的)到服务器或者虚拟机上的docker

    目录 一.前言 二.现在开始分享一下安装历程: 一.前言 最近写了个项目,前端还没写,需要部署到服务器给女朋友实现前端,可是不熟悉Linux的我,蹑手蹑脚,真的是每一步都是bug,可谓是步步维艰,对世 ...

  4. 木字楠后台管理系统开发(1):SpringBoot项目初始化并引入基础文件

  5. SpringBoot项目开发实录

    记录开发一个完整SpringBoot项目的过程. 一.项目初始化 1.1 使用 Liquibase作为数据库迁移工具 第一步:引入 Liquibase 依赖: <!-- 数据库迁移工具: liq ...

  6. 从零开始搭建SpringBoot项目(三)——小程序Uni-app项目搭建(详细教程和实战)

    前情回顾 从零开始搭建SpringBoot项目(一)--开发环境搭建 从零开始搭建SpringBoot项目(二)--Swagger接口测试平台搭建 目录 前情回顾 一.前置条件 二.本篇介绍 三.获取 ...

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

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

  8. springboot项目推荐的打包方式以及springboot项目的瘦身!!!!

    1.springboot项目推荐的打包方式 springboot有两种打包方式,分别是打包成war包和打包成jar包.打包成war包放在tomcat的webapp目录下,随着tomcat的启动,即可访 ...

  9. SpringBoot项目打包发布

    SpringBoot项目打包发布 SpringBoot打包发布jar jar类型项目可以打成jar包 打包流程 导入SpringBoot打包插件 将项目导出成jar包并更新pom文件 使用maven ...

最新文章

  1. pycharm debug code -1073741819
  2. 字节字符区别Java_【JAVA基础】字符数组与字节数组的区别
  3. 北电PBX资料_LD 24 進線直撥功能設定
  4. go加载python_python培训 | python调用go语言来提速
  5. 初入C++(二)类和对象,构造函数,析构函数
  6. 人工智能+人=强大的网络安全
  7. C语言输出规定长度的整数,不够位数前面补零
  8. shell学习之定时运行作业
  9. 给大家推荐一款非常好用的表单验证插件:lr-verify.js
  10. 跨部门的bug的沟通
  11. 华为PUSH SDK 接入方法
  12. 【深度学习】(1) CNN中的注意力机制(SE、ECA、CBAM),附Pytorch完整代码
  13. 力扣刷题 DAY_61 回溯
  14. PS自用(抠图、调色)
  15. MATLAB笔记:打开数据文件的三种方法+读取数据文件的两种方法+保存数据文件的两种方法
  16. 2022-04-清华管理学-清华大学-宁向东
  17. tar.zst 文件格式解压
  18. Linux(Ubuntu)配置防火墙
  19. My97pickerdate设置默认开始日期为当天
  20. 邢台职业技术学院计算机学费,邢台职业技术学院各专业学费标准

热门文章

  1. WPF 把图片分割成两份自动翻页 WpfFlipPageControl:CtrlBook 书控件
  2. 那些年追过的......写过的技术博客
  3. MyEclipse-Web开发时何时重启tomcat、重新部署
  4. 螃蟹学PHP设计模式之解释器模式
  5. Exchange Server 2010安装测试
  6. FPGA优化之高扇出
  7. php网站如何静态化链接,建站教程之网站URL静态化处理
  8. PPStream、PPlive等播放器花屏之解决办法
  9. 从源码分析DEARGUI之add_text_point
  10. nodejs安装部署与运行