Spring Boot + Swagger
http://springfox.github.io/springfox/docs/current/#customizing-the-swagger-endpoints
一、什么是Swagger?
Swagger: 一个描述Restful service规范
可以在界面上测试RestfulService
- swagger-ui: 用来显示API文档
- swagger-editor: 就是一个在线编辑文档说明文件(swagger.json或swagger.yaml文件)的工具,以方便生态中的其他小工具(swagger-ui)等使用
- swagger-validator: 这个小工具是用来校验生成的文档说明文件是否符合语法规定的。用法非常简单,只需url地址栏,根路径下加上一个参数url,参数内容是放swagger说明文件的地址。即可校验。
- swagger-codegen: 代码生成器,脚手架。可以根据swagger.json或者swagger.yml文件生成指定的计算机语言指定框架的代码。
二、Swagger 和 Springfox-Swagger
What is the relationship between swagger-ui and springfox-swagger-ui?
Swagger Spec is a specification.
Swagger Api - an implementation of that specification that supports jax-rs, restlet, jersey etc.
Springfox libraries in general - another implementation of the specification focused on the spring based ecosystem.
Swagger.js and Swagger-ui - are client libraries in javascript that can consume swagger specification.
springfox-swagger-ui - the one that you’re referring to, is just packaging swagger-ui in a convenient way so that spring services can serve it up.
翻译下来就是:
swagger-ui 和 springfox-swagger-ui 的关系是?
Swagger Spec 是一个规范。
Swagger Api 是 Swagger Spec 规范 的一个实现,它支持 jax-rs, restlet, jersey 等等。
Springfox libraries 是 Swagger Spec 规范 的另一个实现,专注于 spring 生态系统。
Swagger.js and Swagger-ui 是 javascript 的客户端库,能消费该规范。
springfox-swagger-ui 仅仅是以一种方便的方式封装了 swagger-ui ,就是可以显示文档内容
三、Springboot 与Swagger集成
1. 增加下面maven 引用
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.6.1</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.6.1</version></dependency>
2. 增加一个Swaagger configuration
package com.example.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;@Configuration
public class Swagger2UiConfiguration extends WebMvcConfigurerAdapter
{@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("org.springframework.boot")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("springboot利用swagger构建api文档").description("简单优雅的restful风格,http://XX").termsOfServiceUrl("http://XXx").version("1.0").build();}}
3.Spring Boots Application 类增加 @EnableSwagger2
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication
@EnableSwagger2
public class UserApplication1 {public static void main(String[] args) {SpringApplication.run(UserApplication1.class, args);}
}
4. 在Controller 中增加相对应的API注释
package com.example.demo;import java.util.concurrent.atomic.AtomicLong;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;@RestController
public class GreetingController {private static final String template = "Hello, %s!";private final AtomicLong counter = new AtomicLong();@ApiOperation(value="获取Greeting info", notes="根据name获取greeting info")@ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "path")@RequestMapping("/greeting")public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {return new Greeting(counter.incrementAndGet(), String.format(template, name));}
}
部署启动服务器
访问: http://localhost:8080/swagger-ui.html#
四、Swagger注解
swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。
- @Api:修饰整个类,描述Controller的作用
- @ApiOperation:描述一个类的一个方法,或者说一个接口
- @ApiParam:单个参数描述
- @ApiModel:用对象来接收参数
- @ApiProperty:用对象接收参数时,描述对象的一个字段
- @ApiResponse:HTTP响应其中1个描述
- @ApiResponses:HTTP响应整体描述
- @ApiIgnore:使用该注解忽略这个API
- @ApiError :发生错误返回的信息
- @ApiImplicitParam:一个请求参数
- @ApiImplicitParams:多个请求参数
Spring Boot + Swagger相关推荐
- Spring Boot swagger之前后端分离
前后端分离详解 现在的趋势发展,需要把前后端开发和部署做到真正的分离做前端的谁也不想用Maven或者Gradle作为构建工具做后端的谁也不想要用Grunt或者Gulp作为构建工具 前后端需要通过接口来 ...
- Spring Boot和Swagger UI
我已经一年没有从头开始开发Spring Web应用程序了,如果我不参加QA自动化工程师的培训,那么这段时间甚至会更长. 由于这个原因,我开发了一个示例REST应用程序. 除了Swagger,一切对我来 ...
- java+swagger+侵入_Spring boot+Swagger配置无侵入式Restful接口(一)
最近一直遇到有人问我spring boot +swagger怎么配置无侵入式的restful接口,这段时间一直很忙,没有时间,刚好星期六,趁着休息时间给大家写个demo,在这里我就粘贴一些步骤,具体的 ...
- 《Spring Boot开发:从0到1》大纲结构
<Spring Boot开发:从0到1> 大纲结构v2.0 第一部分Spring Boot基础 第1章 Spring Boot史前简述 1.1 J2EE(Java 2 Platform E ...
- Spring Boot实现任意位置的properties及yml文件内容配置与获取
〇.参考资料 1.Spring Boot 中文乱码问题解决方案汇总 https://blog.51cto.com/u_15236724/5372824 2.spring boot读取自定义配置prop ...
- Spring Boot集成Swagger导入YApi@无界编程
接口APi开发现状 现在开发接口都要在类似YApi上写文档,这样方便不同的团队之间协作,同步更新接口,提高效率. 但是如果接口很多,你一个个手工在YApi去录入无疑效率很低. 如果是使用Spring ...
- spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例...
本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...
- Spring Boot 2.x基础教程:Swagger静态API文档的生成
点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 作者 | 翟永超 来源 | didispace.com/spring-boot-learni ...
- Spring Boot 集成 Swagger 生成 RESTful API 文档
原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...
最新文章
- 二叉树深度优先 java_二叉树遍历(前序、中序、后序、层次、深度优先、广度优先遍历) java实现...
- 从海外IDC四大标签来看国内IDC公司估值差异问题
- gbk文件转为utf8文件
- Same Sum Blocks
- 【Boost】boost库中thread多线程详解11——线程的休眠和中断
- iOS开发Swift篇—(三)字符串和数据类型
- Java发送HttpRequest
- 如何将word图片粘贴到动易CMS里面
- html table 斜线表头,Table表格加斜线表头
- python调用def函数的格式_Python 函数def 模块调用函数方法及全局变量
- 生日祝福模板html,生日祝福语模板
- 【二次元的CSS】—— 用 DIV + CSS3 画咸蛋超人(详解步骤)
- labview与PLC通讯
- Laravel 速查表 Cache
- WebRTC学习实现视频
- android 酷狗demo_在Android上使用酷狗歌词API
- 艾司博讯:拼多多商品怎样多件一起付款?
- QQ用户这两个文件夹要定时清理
- 源代码和目标代码之间的区别
- python哪个方向最容易上手_Python编程哪个方向容易上手
热门文章
- go 语言系列(二)基本数据类型和操作符
- Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别
- C/C++语言简介之关键字
- 人工智能对医疗和健康产业的冲击和革命——体外克隆
- animate.css(第三方动画使用方法)
- 利用WindowsPhone7_SDK_Full.rar_for_xp,在xp下安装sdk,部署xap软件的教程
- AlertBox 弹出层(信息提示框)效果
- Verilog定义计算位宽的函数clogb2
- delphi fastreport 动态加载图片
- 【Android开发】XML文件解析