Spingboot整合Swagger2

随着互联网技术的发展,一般开发都是前后端分离,那么前端和后端的唯一联系,变成了API接口;API文档变成了前后端开发人员联系的纽带,变得越来越重要,没有API

文档工具之前,大家都是手写API文档的,在什么地方书写的都有,有在confluence上写的,有在对应的项目目录下readme.md上写的,每个公司都有每个公司的玩法,无所谓好

坏。但都有一个很大的诟病就是,如果你的接口改动了,那你必须记得去改你的API文档,而Swagger并不需要。swagger就是一款让你更好的书写API文档的框架。

一、项目搭建

1、pom.xml

        <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

2、application.yml

server:
port: 8086 #代表当前环境是dev环境
spring:
profiles:
active: dev

3、Swagger2DevConfig(配置类)

注意添加@EnableSwagger2注解,这个注解也可以加在springboot启动类上

@Configuration
@EnableSwagger2
public class Swagger2DevConfig { //默认pro环境
@Profile({"default", "pro"})
@Bean
public Docket createWebApi() {
return new Docket(DocumentationType.SWAGGER_2).enable(false).select().build();
} //dev环境接口访问地址:http://localhost:8086/swagger-ui.html
@Profile("dev")
@Bean
public Docket createWebApiDev() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfoDev()).select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build();
} //测试环境接口访问地址: test.jincou.com/user/swagger-ui.html
@Profile("test")
@Bean
public Docket createWebApiTest() {
return new Docket(DocumentationType.SWAGGER_2).host("test.jincou.com/user")
.protocols(Sets.newHashSet("https")).apiInfo(apiInfoDev()).select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build();
} private ApiInfo apiInfoDev() {
return new ApiInfoBuilder().title("用户模块API").description(
"用户api接口文档\n" + "\n" + "测试环境:https://test.jincou.com/user\n").contact(new Contact("张三", "", ""))
.version("1.0").build();
}
}

4、PersonController

这里提供了两个接口

@Api(tags = "用户相关接口")
@RestController
@RequestMapping(value = "/web/api/person")
public class PersonController { @ApiOperation(value = "用户详细信息", notes = "通过id获得用户详细信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "query", defaultValue = "0")})
@RequestMapping(value="page",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
public Person getById(@RequestParam(value="id",defaultValue = "0") Long id){
return new Person(id,2,"小小","杭州");
} //大咖分页列表
@ApiOperation(value = "删除用户信息", notes = "通过ID删除用户信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "query", defaultValue = "0")})
@RequestMapping(value="delete",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
public String delete(@RequestParam(value="id",defaultValue = "0") Long id){ return "删除成功";
}
}

5、Person实体类

@ApiModel(description = "用户详细信息")
public class Person { @ApiModelProperty(value = "用户Id", position = 1)
private Long id; @ApiModelProperty(value = "用户年龄", position = 2)
private int age; @ApiModelProperty(value = "用户姓名", position = 3)
private String name; @ApiModelProperty(value = "用户所在城市", position = 4)
private String city; public Person(Long id,int age,String name,String city){
this.id=id;
this.age=age;
this.city=city;
this.name=name;
}
//提供get和set方法
}

6、本地启动测试

访问:http://localhost:8086/swagger-ui.html。

我只是偶尔安静下来,对过去的种种思忖一番。那些曾经的旧时光里即便有过天真愚钝,也不值得谴责。毕竟,往后的日子,还很长。不断鼓励自己,

天一亮,又是崭新的起点,又是未知的征程(上校15)

springBoot(12)---整合Swagger2的更多相关文章

  1. Springboot项目整合Swagger2报错

    SpringBoot2.2.6整合swagger2.2.2版本的问题,启动SpringBoot报如下错: Error starting ApplicationContext. To display t ...

  2. springboot项目整合swagger2出现的问题

    swagger需要开放以下uri:/swagger-ui.html/swagger-resources/webjars/csrf/v2 添加swagger后项目报错 Failed to start b ...

  3. SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法

    原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...

  4. SpringBoot整合Swagger2

    相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档. 手写Api文档的几个痛点: 文档需 ...

  5. springboot 整合Swagger2的使用

    Swagger2相较于传统Api文档的优点 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不明确 不能直接在线测试接口,通常需要使用工 ...

  6. SpringBoot整合Swagger2,再也不用维护接口文档了!

    前后端分离后,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却很 ...

  7. SpringBoot整合系列-整合Swagger2

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9959844.html SpringBoot整合Swagger2 步骤 第一步:添加必要的 ...

  8. SpringBoot(七):SpringBoot整合Swagger2

    原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...

  9. springboot+cloud 学习(四)Zuul整合Swagger2

    前言 在微服务架构下,服务是分散的,怎么把所有服务接口整合到一起是我们需要关注的. 下面举例用zuul作为分布式系统的网关,同时使用swagger生成文档,想把整个系统的文档整合在同一个页面上来说明. ...

随机推荐

  1. UOJ#465. 【HNOI2019】校园旅行 其他

    原文链接www.cnblogs.com/zhouzhendong/p/UOJ465.html 前言 tmd并查集写挂,调到自闭. cly和我写挂了同一个地方. 一下救了两个人感觉挺开心. 题解 首先直 ...

  2. notes for lxf(二)

    函数 abs()绝对值 max()返回最大值 raise 后接异常类 引发异常 函数返回多个值其实就是返回一个tuple 函数默认返回None 如果有必要检查参数类型用isinstance() typ ...

  3. 四、OpenStack—glance组件介绍与安装

    一.glance介绍 Glance是Openstack项目中负责镜像管理的模块,其功能包括虚拟机镜像的查找.注册和检索等. Glance提供Restful API可以查询虚拟机镜像的metadata及 ...

  4. 爬虫之scrapy-redis

    redis分布式部署 scrapy框架是否可以自己实现分布式? 不可以原因有两点 其一:因为多台机器上部署的scrapy会各自拥有各自的调度器,这样就使得多台机器无法分配start_urls列表中的u ...

  5. <算法图解>读书笔记:第3章 递归

    第3章 递归 3.1 递归 程序调用自身的编程技巧称为递归( recursion).递归做为一种算法在程序设计语言中广泛应用. 一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法,它通常把一 ...

  6. Do-Now—团队冲刺博客三

    Do-Now-团队 冲刺博客三 作者:仇夏 前言 不知不觉我们的项目已经做了三个多礼拜了,团队冲刺博客也写到了这第三篇,看着一个基本成型的APP安装在自己的手机上,一种喜悦感油然而生.好了,现在来看看 ...

  7. 解决fastJson无序问题

    对外提供接口,第三方传过来的参数没问题.可是用fastJson 转换的出现 参数顺序不一致,导致 验签失败 解决fastJosn 转换无序问题 https://github.com/alibaba/f ...

  8. Vue(二十八)el-cascader 动态加载 - 省市区组件

    1.后台接口为点击加载下一级 ,传省市区id <template> <el-cascader v-model="selectedOptions" placehol ...

  9. [LeetCode] Fibonacci Number 斐波那契数字

    The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...

  10. vue中添加title中的小图标

    webpack.prod.conf.js 这个文件中: 引入代码const path = require('path') :下面是进行配置: new HtmlWebpackPlugin({ filen ...