开发完成的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 。遇到这个问题很有可能就是被拦截了。
这里之所以 写这么少,是因为这些问题我遇到了,也解决了,并且是按照上面思路解决的。
开发完成的springboot项目扩展 swagger的更多相关文章
- SpringBoot项目集成swagger报NumberFormatException: For input string: ""
java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.f ...
- 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就可以正常启动
- eclipse 开发 spring 、 springboot项目调试时一直跳转到 SilentExitExceptionHandler.exitCurrentThread 方法
不想让 eclipse 调试时一直跳转到该方法 解决方法: Eclipse->[Preferences]->[Java]->[Debug]:去掉[Suspend execution ...
- springboot+mybatis-puls利用swagger构建api文档
项目开发常采用前后端分离的方式.前后端通过API进行交互,在Swagger UI中,前后端人员能够直观预览并且测试API,方便前后端人员同步开发. 在SpringBoot中集成swagger,步骤如下 ...
- 【Docker】Maven打包SpringBoot项目成Docker镜像并上传到Harbor仓库(Eclipse、STS、IDEA、Maven通用)
写在前面 最近,在研究如何使用Maven将SpringBoot项目打包成Docker镜像并发布到Harbor仓库,网上翻阅了很多博客和资料,发现大部分都是在复制粘贴别人的东西,没有经过实践的检验,根本 ...
- 从零开始的SpringBoot项目 ( 五 ) 整合 Swagger 实现在线API文档的功能
综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...
- JAVA中自定义扩展Swagger的能力,自动生成参数取值含义说明,提升开发效率
大家好,又见面了. 在JAVA做前后端分离的项目开发的时候,服务端需要提供接口文档供周边人员做接口的对接指导.越来越多的项目都在尝试使用一些基于代码自动生成接口文档的工具来替代由开发人员手动编写接口文 ...
- springboot项目--传入参数校验-----SpringBoot开发详解(五)--Controller接收参数以及参数校验----https://blog.csdn.net/qq_31001665/article/details/71075743
https://blog.csdn.net/qq_31001665/article/details/71075743 springboot项目--传入参数校验-----SpringBoot开发详解(五 ...
- Kotlin开发springboot项目(三)
Kotlin开发springboot项目(三) 在线工具 https://www.sojson.com IDEA中Kotlin生成可执行文件1,项目使用Gradle构建2,在model的build.g ...
随机推荐
- mysql ef vs
https://www.cnblogs.com/dux2013/archive/2017/12/11/8025077.html
- 删除文件中的 ^M 字符
删除文件中的 ^M 字符 有时候,我们在 Linux 中打开曾在 Win 中编辑过的文件时,会在行尾看到 ^M 字符.虽然,这并不影响什么,但心里面还是有点不痛快.如果想要删除这些 ^M 字符,可以使 ...
- springboot运行jar包时候加载指定目录的其他jar支持包
最近发生一个小故障,调试好的项目,发布成jar包后无法找到oracle的驱动,研究了一下解决了.记录一下.写了一个run.sh脚本 #!/bin/bash cd ~ cd app nohup java ...
- PyQt5 & Qt Designer使用小结
开始在知乎写文章的原因,主要还是想整理平时的经验,方便自己以后查看,有机会的话大家也可以交流吧. 11月中旬由于项目需要,和另一名实习生负责使用Python开发一个数据分析的小软件. 虽然才开始接触Q ...
- Spring boot之JPA
JPA 步骤: (1)在pom.xml添加mysql,spring-data-jpa依赖 (2)在application.properties文件中配置mysql连接配置文件 (3)在applicat ...
- python3笔记十三:python数据类型-Set集合
一:学习内容 集合概念 集合创建 集合添加 集合插入 集合删除 集合访问 集合操作:并集.交集 二:集合概念 1.set:类似dict,是一组key的集合,不存储value 2.本质:无序和无重复元素 ...
- 微信小程序底部菜单栏的使用方法
1.找到项目根目录的配置文件 app.json,在配置文件中加入配置代码.例如: "tabBar": { <!--底部的导航配置属性--> "color&qu ...
- LeetCode 54. 螺旋矩阵(Spiral Matrix) 剑指offer-顺时针打印矩阵
题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, ...
- 一、基础篇--1.1Java基础-Session和Cookie的区别【转】
https://www.cnblogs.com/zlw-xf/p/8001383.html 1:cookie数据存放在客户的浏览器上(客户端),session数据放 @1:cookie不是很安全,别人 ...
- C++多重继承实践
实践代码如下: #include <iostream> using namespace std; class Animal { private: int hash; public: Ani ...