spring boot 2整合swagger-ui
1.添加mvn依赖
修改pom.xml
加入
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
2.创建配置类
在Application.java
同级创建Swagger2的配置类Swagger2
package com.tydt.decision; 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; @Configuration
@EnableSwagger2
public class Swagger2{
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.tydt.decision.controller"))
.paths(PathSelectors.any()).build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Decision Manage Swagger RESTful APIs")
.description("Decision API")
.termsOfServiceUrl("http://swagger.io/")
.contact(new Contact("Beibei", "127.0.0.1", "XXXXXXX@qq.com"))
.version("1.0")
.build(); } }
注:
- 如果出现下面情况
引入需要的包
但是启动时又出现Error creating bean with name 'apiDocumentationScanner' defined in URL
这是由于swagger依赖google的guava,需要添加依赖,而当前项目的guava版本与之不匹配,修改为
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
- http://localhost:8090/swagger-ui.html页面没显示
这是因为Spring Boot自动配置本身不会自动把/swagger-ui.html这个路径映射到对应的目录META-INF/resources/下面。加上这个映射即可
@Configuration
public class WebMvcConfig implements WebMvcConfigurer { ……
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/"); }
}
解决了上面出现的问题,访问http://localhost:8090/swagger-ui.html就能看到下面的页面了
说明:
(1)通过@Configuration
注解,让Spring来加载该类配置
(2)再通过@EnableSwagger2
注解来启用Swagger2
(3)通过createRestApi
函数创建Docket
的Bean之后,apiInfo()
用来创建该Api的基本信息
(4)select()
函数返回一个ApiSelectorBuilder
实例用来控制哪些接口暴露给Swagger来展现
(5)指定扫描的包路径来定义,会扫描该包下所有Controller定义的API,并产生文档内容,除了用@ApiIgnore指定的
(6)通过@ApiOperation
注解来给API增加说明
(7)通过@ApiImplicitParams
(8)@ApiImplicitParam注解来给参数增加说明
spring boot 2整合swagger-ui的更多相关文章
- Spring Boot:整合Swagger文档
综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...
- Spring Boot 快速整合Swagger
一.前言 Spring Boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...
- Spring Boot 2 整合Swagger简单入门
Swagger是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. 1.pom.xml添加配置 可以到http://mvnrepository.com上搜索springfox,便可以看到Sp ...
- 【Spring Boot&&Spring Cloud系列】Spring Boot项目集成Swagger UI
前言 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集 ...
- Spring Boot:整合Swagger
1.先创建一个SpringBoot项目 其中application.properties文件中是创建项目时自动添加的配置. 2.添加相关maven依赖 <!--swagger--> < ...
- Spring Boot:整合Spring Security
综合概述 Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架.除了常规的认证(Authentication)和授权(Author ...
- Spring Boot:整合MyBatis框架
综合概述 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单 ...
- Spring Boot:整合JdbcTemplate
综合概述 Spring对数据库的操作在jdbc上面做了更深层次的封装,而JdbcTemplate便是Spring提供的一个操作数据库的便捷工具.我们可以借助JdbcTemplate来执行所有数据库操作 ...
- Spring Boot:整合Spring Data JPA
综合概述 JPA是Java Persistence API的简称,是一套Sun官方提出的Java持久化规范.其设计目标主要是为了简化现有的持久化开发工作和整合ORM技术,它为Java开发人员提供了一种 ...
- Spring Boot:整合Shiro权限框架
综合概述 Shiro是Apache旗下的一个开源项目,它是一个非常易用的安全框架,提供了包括认证.授权.加密.会话管理等功能,与Spring Security一样属基于权限的安全框架,但是与Sprin ...
随机推荐
- CocosCreator 场景转换API
一.CocosCreator场景的切换API: cc.director.loadScene( ‘场景名称’);//场景切换必须使用的API 通过场景名称进行加载场景时,常用的就是上边的API,但是版本 ...
- git配置SSH Key,上传本地代码至github
git配置全局的name和email git config --global user.name "name" git config --global user.email &qu ...
- spring源码分析系列 (3) spring拓展接口InstantiationAwareBeanPostProcessor
更多文章点击--spring源码分析系列 主要分析内容: 一.InstantiationAwareBeanPostProcessor简述与demo示例 二.InstantiationAwareBean ...
- MySql md5加密 sqlserver md5加密 C# md5加密 java md5加密
便民md5加密: 百度md5加密: MySQL 加密语法: MD5(加密字符串) -- 中文加密 SELECT MD5('你好') -- 中文加密匹配查询 SELECT * FROM 表名 WHERE ...
- 伪造请求IP
//随机IP function Rand_IP(){ $ip2id= round(rand(600000, 2550000) / 10000); //第一种方法,直接生成 $ip3id= round( ...
- Java 对象的序列化和反序列化
先创建一个实现了Serializable接口的对象 import java.io.Serializable; /** * 可序列化Person对象. * @author Ramer * Sep 18, ...
- HDU4772(杭州赛区)
Zhuge Liang's Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- ArcGIS教程:曲率
摘要 计算栅格表面的曲率,包括剖面曲率和平面曲率. 用法 · 主要输出结果为每个像元的表面曲率,该值通过将该像元与八个相邻像元拟合而得.曲率是表面的二阶导数,或者可称之为坡度的坡度.可供选择的输出曲率 ...
- Visio画流程图风格设置
第一步:选取设计下选用“简单” 第二步:设置颜色为“铅笔” 第三步:设置效果为“辐射” 第四步:效果
- [Canvas]飞机飞越河谷(背景向下平滑移动)
请点击此处下载代码并用Chrome浏览器打开观看. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> <meta ...