博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用springfox整合SpringMVC和Swagger
阅读量:4069 次
发布时间:2019-05-25

本文共 3682 字,大约阅读时间需要 12 分钟。

Swagger 是一系列对 RESTful 接口进行规范描述和页面展示的工具. 通过 springfox-swagger 将 Swagger 与 Spring-MVC 整合, 可从代码中的注解获取信息, 并生成相应的文档. 效果如下所示.

目前 Swagger 的 api 版本规范已经更新到 2.0 版本, 中文网络上基本上都是 1.0 的 api 版本规范的教程. 捣鼓了一天终于搞定了, 这两者区别还是有的.

先添加依赖

io.springfox
springfox-swagger2
2.5.0
io.springfox
springfox-swagger-ui
2.5.0
com.fasterxml.jackson.core
jackson-annotations
${version.jackson}
com.fasterxml.jackson.core
jackson-databind
${version.jackson}
com.fasterxml.jackson.core
jackson-core
${version.jackson}
io.springfox
springfox-petstore
2.5.0
junit
junit
4.11
test
log4j
log4j
1.2.17
jar
org.springframework
spring-core
${version.spring}
org.springframework
spring-web
${version.spring}
org.springframework
spring-webmvc
${version.spring}
org.springframework
spring-beans
${version.spring}
org.springframework
spring-context
${version.spring}
org.springframework
spring-jdbc
${version.spring}
org.springframework
spring-context-support
${version.spring}
org.springframework
spring-test
${version.spring}
test
javax.servlet
javax.servlet-api
3.1.0
org.slf4j
slf4j-log4j12
1.7.5
org.slf4j
slf4j-api
1.7.5

springfox-swagger2 可以将代码中的注解转换为符合 Swagger 的 API 规范的 swagger.json 文件, springfox-swagger-ui 提供了将 swagger.json 转换为 html 页面的服务.

最精简的 springfox 配置

Docket 对象为 spring-fox 提供了配置信息, ApiInfo 为生成的文档提供了元数据信息. 两者都使用这里我们仅仅使用最小配置, 两者均为默认配置. @EnableSwagger2 表示启用 Swagger.

@Configuration@EnableSwagger2public class MySwaggerConfig {}
然后再在 spring-component.xml 配置文件中将这个类注册成 bean, 用于启用配置

这样就开启了 springfox 和 swagger.

spring-mvc.xml:

springfox 的注解

写一个简单的 Controller

@Api(value = "User控制器")@Controller@RequestMapping("/user")public class UserController {    @ApiOperation(value = "根据用户id查询用户信息", httpMethod = "GET", produces = "application/json")    @ApiResponse(code = 200, message = "success", response = Result.class)    @ResponseBody    @RequestMapping(value = "queryUserById", method = RequestMethod.GET, produces = "application/json")    public Result queryUserById(@ApiParam(name = "userId", required = true, value = "用户Id") @RequestParam("userId") int userId, HttpServletRequest request) {        User user = new User(userId, "haoyifen", 24);        Result result = new Result();        result.setCode(0);        result.setData(user);        result.setMessage("success");        return result;    }}

常用的几个用于生成文档的注解如下:

- @Api 表示该类是一个 Swagger 的 Resource, 是对 Controller 进行注解的
- @ApiOperation 表示对应一个 RESTful 接口, 对方法进行注解
- @ApiResponse 表示对不同 HTTP 状态码的意义进行描述
- @ApiParam 表示对传入参数进行注解

结果验证:

最后工程结构如下:

访问 就可以看到API文档, 并且能够进行在线的操作.

相关资源:

springfox官方文档:

你可能感兴趣的文章
大数据入门:Scala函数式编程
查看>>
【数据结构周周练】002顺序表与链表
查看>>
C++报错:C4700:使用了非初始化的局部变量
查看>>
【数据结构周周练】003顺序栈与链栈
查看>>
C++类、结构体、函数、变量等命名规则详解
查看>>
C++ goto语句详解
查看>>
【数据结构周周练】008 二叉树的链式创建及测试
查看>>
《软件体系结构》 第九章 软件体系结构评估
查看>>
《软件体系结构》 第十章 软件产品线体系结构
查看>>
《软件过程管理》 第六章 软件过程的项目管理
查看>>
《软件过程管理》 第九章 软件过程的评估和改进
查看>>
《软件过程管理》 第八章 软件过程集成管理
查看>>
分治法 动态规划法 贪心法 回溯法 小结
查看>>
《软件体系结构》 练习题
查看>>
《数据库系统概论》 第一章 绪论
查看>>
《数据库系统概论》 第二章 关系数据库
查看>>
《数据库系统概论》 第三章 关系数据库标准语言SQL
查看>>
SQL语句(二)查询语句
查看>>
SQL语句(六) 自主存取控制
查看>>
《计算机网络》第五章 运输层 ——TCP和UDP 可靠传输原理 TCP流量控制 拥塞控制 连接管理
查看>>