第一步: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. 批量查询 xml的方式 还一种是表变量

    var adds1 = getoneCityList.Select(l => { return new { YDCode = l.YDCode, SJQH = l.SJQH }; });var ...

  2. ​直接插入排序(Straight Insertion Sort)

    1.定义 直接插入排序(Straight Insertion Sort)的基本操作是将一个记录插入到已经排好序的有序表中,从而得到一个新的.记录数增1的有序表. 插入排序(Insertion Sort ...

  3. POJ 3068 运送危险化学品 最小费用流 模板题

    "Shortest" pair of paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1215 ...

  4. 【Python】爬虫汇总

    主要流程: 获取url下载网页从网页中找寻自己需要的保存(解析+输出)主要概念URL:分大小写统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址.互联 ...

  5. jquery 动态增加table行,动态删除table行

    在html中我们大量的用到了局部刷新,局部刷新就是使用js来动态的修改html局部的数据.下面来介绍下使用jquery来动态的增加table的行,当然这种方式可以适用到任何的html组件中去. < ...

  6. Redis Cluster in Ubuntu

    1. 首先,进到Redis-server 的位置,确认 Redis server 可以正常启动 2. 在 redis-5.0.3 目录下创建文件夹 redisCluster_Demo_byMe,并在  ...

  7. LeetCode 3. 无重复字符的最长子串(Longest Substring Without Repeating Characters)

    题目描述 给定一个字符串,找出不含有重复字符的最长子串的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. ...

  8. JAVA TCP Socket

    服务器端   package com.Pong.tcpip; import java.io.BufferedReader; import java.io.IOException; import jav ...

  9. leetcode-easy-array-136. Single Number

    mycode   75.80% class Solution(object): def singleNumber(self, nums): """ :type nums: ...

  10. MySQL MGR 单主模式下master角色切换规则

    MGR单主模式,master节点可读可写,其余节点都是只读.当配置MGR为单主模式,非master节点自动开启super_read_only 当可读可写的节点异常宕机,会进行怎样的切换?在选择新的可写 ...