SpringBoot+Swagger2 整合
SpringBoot+Swagger2四步整合
第一步:添加相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<swagger2.version>2.7.0</swagger2.version>
</properties>
<dependencies>
<!--springBoot 相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--swagger2 相关依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger2.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger2.version}</version>
</dependency>
</dependencies>
创建SrpingBoot启动类
/**
* Springboot+Swagger整合启动类
*
* @author Y.yang
* @date 2019/3/12
*/
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
第二步:配置Swagger2
注意添加@Configuration
EnableSwagger2
注解
/**
* Swagger2 接口Api文档 配置文件
*
* @author Y.yang
* @date 2019/3/12
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* 初始化创建Swagger Api
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
// 详细信息定制
.apiInfo(apiInfo())
.select()
// 指定当前包路径
.apis(RequestHandlerSelectors.basePackage("com.fame.controller"))
// 扫描所有 .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
/**
* 添加摘要信息
*/
private ApiInfo apiInfo() {
// 用ApiInfoBuilder进行定制
return new ApiInfoBuilder()
.title("标题:springBoot-Swagger2整合学习")
.description("描述:文档构建器")
.contact(new Contact("Fame-springBoot-Swagger2", null, null))
.version("版本号: 1.0")
.build();
}
}
springfox为我们提供了一个Docket(摘要的意思)类,我们需要把它做成一个Bean注入到spring中,
显然,我们需要一个配置文件,并通过一种方式(显然它会是一个注解)告诉程序,这是一个Swagger配置文件。
springfox允许我们将信息组合成一个ApiInfo的类,作为构造参数传给Docket(当然也可以不构造这个类,而直接使用null,但是你的这个API就太low了)。
第三步:创建测试实例
/**
* Swagger接口测试
*
* @author Y.yang
* @date 2019/3/12
*/
@RestController
public class UserController {
@GetMapping("/get")
public String get(){
return "Hello Swagger2";
}
}
第四步:输入SwaggerUI地址
http://localhost:8080/swagger-ui.html
Swagger2基本使用-常用注解
接口/方法常用注解
/**
* Swagger接口测试
*
* @author Y.yang
* @date 2019/3/12
*/
@Api(value = "用户信息", tags = { "用户信息" })
@RestController
public class UserController {
@ApiOperation(value = "用户信息分页查询")
@GetMapping("/page")
public String page(User user) {
return "Hello Swagger2";
}
@ApiOperation(value = "用户信息查询")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
@ApiImplicitParam(name = "file", value = "文件导入", required = true, dataType = "MultipartFile")
})
@GetMapping("/id")
public UserVo getUser(Long id, MultipartFile file) {
return new UserVo();
}
}
@Api: 描述类/接口的主要用途
用于类;表示标识这个类是swagger的资源
tags–表示说明
value–也是说明,不会显示在接口文档上,可以使用tags替代
但是tags如果有多个值,会生成多个list
@Api(value = "用户信息", tags = { "用户信息" })
@ApiOperation: 描述方法用途
@ApiOperation(value = "用户信息分页查询")
@ApiImplicitParam: 描述方法的参数
@ApiImplicitParams: 描述方法的参数(Multi-Params)
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
@ApiImplicitParam(name = "file", value = "文件导入", dataType = "MultipartFile")
})
实体类常用注解
/**
* 用户信息 数据传输对象 Dto(Data Transfer Object)
*
* @author Y.yang
* @date 2019/3/29
*/
@ApiModel(description = "用户信息请求对象")
@Data
public class User implements Serializable {
private static final long serialVersionUID = -6986638131456347054L;
@ApiModelProperty(value = "姓名")
private String username;
@ApiModelProperty(value = "性别")
private String sex;
@ApiModelProperty(value = "年龄")
private Integer age;
}
@ApiModel:描述实体类(Dto、Vo、Do等)
@ApiModel(description = "用户信息请求对象")
@ApiModelProperty:描述实体类的字段
@ApiModelProperty(value = "姓名")
SpringBoot+Swagger2 整合的更多相关文章
- SpringBoot与Swagger2整合
一.Swagger简介与优势 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还为了以后交接方便,都有要求写API文档. Swa ...
- SpringBoot+Swagger整合API
SpringBoot+Swagger整合API Swagger:整合规范的api,有界面的操作,测试 1.在pom.xml加入swagger依赖 <!--整合Swagger2配置类--> ...
- 学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用
学习SpringBoot,整合全网各种优秀资源,SpringBoot基础,中间件,优质项目,博客资源等,仅供个人学习SpringBoot使用 一.SpringBoot系列教程 二.SpringBoot ...
- SpringMVC中使用Swagger2整合
Swagger2是什么 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 W ...
- springboot+swagger2
springboot+swagger2 小序 新公司的第二个项目,是一个配置管理终端机(比如:自动售卖机,银行取款机)的web项目,之前写过一个分模块的springboot框架,就在那个框架基础上进行 ...
- SpringBoot 同时整合thymeleaf html、vue html和jsp
问题描述 SpringBoot如何同时访问html和jsp SpringBoot访问html页面可以,访问jsp页面报错 SpringBoot如何同时整合thymeleaf html.vue html ...
- SpringBoot+AOP整合
SpringBoot+AOP整合 https://blog.csdn.net/lmb55/article/details/82470388 https://www.cnblogs.com/onlyma ...
- SpringBoot+Redis整合
SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...
- springboot+maven整合spring security
springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...
随机推荐
- T-SQL判断是否存在表、临时表
利用SQL SERVER的系统函数 object_id() 可以判断是否存在表.临时表, object_id() 的作用是返回架构范围内对象的数据库对象标识.(即返回系统视图 sys.objects ...
- 【转】Java并发编程:线程池的使用
Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...
- [Uliweb]-URL映射
URL映射¶ Uliweb使用Werkzeug的Routing来进行URL的处理.当你使用manage.py的makeapp命令生成一个新 的App时,它会自动生成views.py文件,其中会自动从u ...
- vhosetuser 和 vhostuservlient 差异
Open vSwitch支持的vHost-user类型 在Open vSwitch中vHost User通过socket进行通信,模式为client-server,其中server端负责创建/管理/销 ...
- Python爬虫之关于登录那些事
常见的登录方式有以下两种: 查看登录页面,csrf,cookie;授权:cookie 直接发送post请求,获取cookie 上面只是简单的描述,下面是详细的针对两种登录方式的时候爬虫的处理方法 第一 ...
- vue中axios访问Java后端跨域问题解决
问题背景: 前后端分离,前端选用Vue,后端选用Java,vue编译出的静态页面采用ngix发布,在前端访问后端时出现跨域问题. 解决方法: 跨域的问题解决方法有好多种,这里是通过服务端解决,以下是代 ...
- 使用SUI框架下的<a>标签点击跳转页面不刷新的问题
最近写好了几个页面,今天试着将各个页面的链接打通,然后问题就来了...(╯︵╰) 这里看一下原来想要实现的两个页面跳转的效果--点击图一标注的栏目可以跳转到一个新的页面图二... 按照之前写a标签的跳 ...
- function参数
expr_without_variable { Z_LVAL($$.u.constant) = ; zend_do_pass_param(&$, ZEND_SEND_VAL, Z_LVAL($ ...
- 二叉排序树的理解和实现(Java)
二叉排序树的定义和性质 二叉排序树又称二叉排序树.它或者是一个空树,或者是一个具有下列性质的二叉树: 若它的左子树不空,则左子树上所有节点的值均小于它的根结构的值 若它的右子树不空,则右子树上所有结点 ...
- AngularJS入门之动画
AngularJS中ngAnimate模块支持动画效果,但是ngAnimate模块并未包含在AngularJS核心库中,因此需要使用ngAnimate需要在定义Module时声明对其的引用. Angu ...