Swagger框架学习分享
2016-06-13 17:01
323 查看
Created and last modified by 刘新宇 大约1分钟以前
转至元数据起始
一、背景介绍
1.1.项目简介
1.2.code
repository
1.3.演示项目
二、开发准备
2.1.环境准备
2.2.项目搭建
2.2.1.jar仓库
2.2.2.相关依赖
2.2.3.编写配置文件
2.2.4.与swagger-ui集成
2.6.5.Controller配置
2.2.6.启动中间件
2.2.7.需求定制
三、学习感想
一、背景介绍
1.1.项目简介
Swagger项目是由Dilip Krishnan和Adrian Kelly等人维护开发的一个为Spring Web MVC 项目提供方法文档的一个框架。该框架最主要的功能是将Controller的方法进行可视化的展现,像方法注释,方法参数,方法返回值等都提供了相应的用户界面,尤其是对JSON参数的支持。同时可以结合swagger-ui可以对用户界面进行不同程度的定制,也可以对方法进行一个简单的测试。
1.2.code repository
github:https://github.com/springdox/springdoxmaven:http://www.mvnrepository.com/artifact/com.mangofactory/swagger-springmvc
1.3.演示项目
官方:https://github.com/adrianbk/swagger-springmvc-demo民间:https://github.com/qq291462491/bugkillers
二、开发准备
2.1.环境准备
idea intellij 13+Oracle java 1.6
Gradle 2.0 +
2.2.项目搭建
2.2.1.jar仓库
Maven
2.2.2.相关依赖
As of v0.9.5 all dependencies on scala have been removed.Spring 3.2.x or above
jackson 2.4.4
guava 15.0
2.2.3.编写配置文件
编写一个Java文件,并使用注解:@Configuration 配置注解,自动在本类上下文加载一些环境变量信息
@EnableWebMvc
@EnableSwagger 使swagger生效
@ComponentScan("com.myapp.packages") 需要扫描的包路径
示例:
2.2.4.与swagger-ui集成
方式一:Note: Only use this option if you don‘t need to customize any of the swagger-ui static content, otherwise use option 2.
Use the web-jar which packages all of the swagger-ui static content.
Requires that your app is using the servlet 3 specification.
For non-spring boot applications some extra spring configuration (ResourceHandler‘s) is required. See: https://github.com/adrianbk/swagger-springmvc-demo/tree/master/swagger-ui
Manually copy all of the static content swagger-ui‘s dist directory (https://github.com/wordnik/swagger-ui/tree/master/dist)
Provide the necessary view resolvers and resource handlers to serve the static content.
Consult the spring documentation on serving static resources.
The following is one way to serve static content from /src/main/webapp
2.6.5.Controller配置
使用注解对Controller进行配置:@Api 配置方法API
@ApiOperation API的操作 GET PUT DELETE POST
@ApiParam API的方法参数描述
示例Controller:
2.2.6.启动中间件
项目配置了Jetty或者Tomcat等Web容器的话,在对应的Controller配置好的话就可以启动看效果了。访问本机:http://127.0.0.1:9081/api
远程示例:http://115.29.170.213/api
2.2.7.需求定制
分组信息定制Url定制
Http相应定制
三、学习感想
Swagger很好的为我们在开发RESTful框架应用时,前后台分离的情况下提供了很有效的解决方案,上手迅速,操作简单,界面精简,功能完善,满足各种定制化的需求,是在使用Spring MVC做Web开发时的不二选择。通过对swagger的学习,增强了英语交流的能力,改变了以前的学习方法,收获了很多,同时也也得感谢国外友人的悉心帮助~技术无国界~
在公司的Wiki上写的博文,因为外面访问不了公司的内网,故贴过来给需要的小伙伴们分享一下。
相关文章推荐
- Springfox swagger-ui 覆盖默认request host
- swagger入门及swagger-maven-plugin使用
- Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
- 在Laravel5.2中使用dingo+JWT+Swagger
- swagger 的使用
- SpringMVC整合Swagger框架
- swagger 与 springboot 的整合实战
- Swagger 与 SpringMVC 整合的步骤
- Restful形式接口文档生成之Swagger与SpringMVC整合手记
- Swagger+Spring MVC框架学习分享
- swagger使用
- RESTful API文档生成的开源项目Swagger
- Swagger-UI的配置与使用
- Swagger webservice doc generation
- SSM配置swagger
- spring boot+swagger 生成Restful风格API
- Swagger+Spring mvc生成Restful接口文档
- 常见swagger注解一览与使用
- 微服务开发架构——Spring Cloud常见问题与总结<一>Eureka常见问题
- Swagger 配置