基于SpringBoot的Swagger2快速入门
1. Springboot 集成 Swagger2
1.1 导入Swagger2 依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
1.2 编写 Swagger 配置:如下简单添加注解即可访问SwaggerUi
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
1.3 访问http://localhost:8080/swagger-ui.html

2. 配置Swagger2
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* 配置Swagger的Docket实例
* @return
*/
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(false) //关闭swagger
.groupName("xxx") //分组,多个分组需要多个Docket实例
.select() //接口过滤,扫描包名下的接口
.apis(RequestHandlerSelectors.basePackage("cn.lcx.demo.controller"))
.build();
}
/**
* 配置Swagger信息
* @return
*/
private ApiInfo apiInfo(){
Contact contact = new Contact("xxx","http://localhost:8080","xxxx@qq.com");
return new ApiInfo(
"xxx团队xxx项目api开发文档",
"xxx团队xxx项目api开发文档",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
访问http://localhost:8080/swagger-ui.html

3. 编写Api文档注释
3.1 实体类
@ApiModel("用户实体类") //注解在类上的api文档
public class User {
@ApiModelProperty("用户名") //注解在属性上的api文档
private String username;
@ApiModelProperty("密码")
private String password;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
查看ui下面的model

3.2 接口类
@RestController
public class HelloController {
@ApiOperation("获取用户接口") //注解在方法名上的api文档
@PostMapping("/user")
//注解在变量上的api文档
public User getUser(@ApiParam("用户名") String username, @ApiParam("密码") String password){
return new User();
}
}
查看ui的接口信息

4. Swagger2 还提供接口在线测试,功能类似postman

基于SpringBoot的Swagger2快速入门的更多相关文章
- 在线Online表单来了!JeecgBoot 2.1 版本发布——基于SpringBoot+AntDesign的快速开发平台
项目介绍 Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台! 采用前后端分离架构:SpringBoot,Ant-Design-Vue,Mybatis,Shiro,JWT. ...
- SpringBoot系列: RestTemplate 快速入门
====================================相关的文章====================================SpringBoot系列: 与Spring R ...
- 基于SpringBoot+AntDesign的快速开发平台,JeecgBoot 2.0.2 版本发布
Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台! 采用前后端分离架构:SpringBoot,Ant-Design-Vue,Mybatis,Shiro,JWT. 强大的代 ...
- Springboot 完整搭建快速入门,必看!
前言 手把手教你Springboot微服务项目搭建快速入门,通过本文学习Springboot的搭建快速入门,掌握微服务大致的配置服务,后续将会继续将核心组件引入到项目中,欢迎关注,点赞,转发. Spr ...
- 基于PHP的cURL快速入门
cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性,以及在PHP中如 ...
- (转)基于PHP的cURL快速入门
1. 原文:基于PHP的cURL快速入门 英文原文:http://net.tutsplus.com/tutorial ... for-mastering-curl/ 原文作者:Burak Guzel ...
- 基于springboot构建dubbo的入门demo
之前记录了构建dubbo入门demo所需的环境以及基于普通maven项目构建dubbo的入门案例,今天记录在这些的基础上基于springboot来构建dubbo的入门demo:众所周知,springb ...
- 基于PHP的cURL快速入门教程 (小偷采集程序)
cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.很多小偷程序都是使用这个函数. 最爽的是,PHP也支持 cURL 库.本文将介绍 c ...
- SpringBoot整合ActiveMQ快速入门
Spring Boot 具有如下特性: 为基于 Spring 的开发提供更快的入门体验 开箱即用,没有代码生成,也无需 XML 配置.同时也可以修改默认值来满足特定的需求. 提供了一些大型项目中常见的 ...
随机推荐
- [NOI2016]区间 题解(决策单调性+线段树优化)
4653: [Noi2016]区间 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1593 Solved: 869[Submit][Status][ ...
- Springboot开篇
1.Spring -boot-starter-web:用于构建web 应用模块,加入后包含spring mvc框架,默认内嵌tomcat容器 2.spring-boot-starter-jpa:用于构 ...
- docker集群故障迁移
docker swarm 故障时候镜像迁移(无法添加新节点的时候)生产docker集群出现了故障,无法正常添加删除节点.在这样的情况下只能想办法把故障集群的镜像迁移到新的docker集群当中.将发生故 ...
- Java获取CPU占用率
原文链接:https://www.jianshu.com/p/015cc4805e29 最近做一个Java性能统计的问题,需要统计当前进程占用CPU的情况,最开始使用Java MxBean来获取 Op ...
- 剑指offer——04二维数组中的查找
题目: 数组中唯一只出现一次的数字.在一个数组中除一个数字只出现一次之外,其他数字都出现了三次.请找出那个只出现一次的数字. 题解: 如果一个数字出现三次,那么它的二进制表示的每一位(0或者1)也出现 ...
- 为什么Netty这么火?与Mina相比有什么优势?
Netty是什么?为什么这么火? Netty是目前最流行的由JBOSS提供的一个Java开源框架NIO框架,Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服 ...
- JDK8新特性之接口默认方法与静态方法
接口默认方法与静态方法 有这样一些场景,如果一个接口要添加一个方法,那所有的接口实现类都要去实现,而某些实现类根本就不需要实现这个方法也要写一个空实现,所以接口默认方法就是为了解决这个问题. 接口静态 ...
- 2019年Pandas官方用户调研
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt %matplotlib inline plt.rcP ...
- MySQL高可用配置(主从复制)
主从复制包含两个步骤: 在 master 主服务器(组)上的设置,以及在 slave 从属服务器(组)上的设置. 环境: MASTER: 192.168.155.101SLAVE: 192.168.1 ...
- nginx : OpenEvent Faied access is denied
主要是naginx做成服务后, 服务运行的用户权限不够(他是system的),而cmd运行的是(Administrator) 所以我们去服务里,修改成以下就好了