Swagger 这一个文章就够了

本文详细介绍了Swagger的基本概念,包括Swagger Edit和Swagger UI的使用,并提供了在SpringMVC项目中快速生成JSON API文件及整合Swagger的步骤,帮助开发者轻松创建和管理RESTful API文档。
摘要由CSDN通过智能技术生成

Swagger快速理解

Swagger:The Best APIs are Built with Swagger Tools
。Swagger可以定义一个标准的RESTful风格的API,与语言无关,是一个API的规范。

Swagger官网:http://swagger.io

GitHub地址:https://github.com/swagger-api

这里,提到Swagger就不得不说说Springfox,Springfox是一个开源的API Doc的框架, 它的前身是swagger-springmvc,可以将我们的Controller中的方法以文档的形式展现。官方定义为: Automated JSON API documentation for API’s built with Spring。Swagger和SpringFox到底什么关系呢?

- Swagger Spec 是一个规范。
- Swagger Api 是 Swagger Spec 规范 的一个实现,它支持 jax-rs, restlet, jersey 等等。
- Springfox libraries 是 Swagger Spec 规范 的另一个实现,专注于 spring 生态系统。
- Swagger.js and Swagger-ui 是 javascript 的客户端库,能消费该规范。
- springfox-swagger-ui 仅仅是以一种方便的方式封装了 swagge
Swagger是一种流行的API(应用程序编程接口)开发工具集,它允许开发人员设计、构建、记录和使用RESTful Web服务。Swagger通常与OpenAPI规范结合使用,这是一种用于描述API的语言。如果你是想用Java语言结合Spring Boot框架和Springfox库来创建Swagger文档并暴露一个API端点,以下是一个简单的示例代码: 首先,你需要在项目中引入Springfox依赖。如果你使用Maven,可以在pom.xml文件中添加以下依赖: ```xml <!-- Swagger --> <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> ``` 然后,创建一个配置类来启用Swagger: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .build(); } } ``` 现在,你可以创建一个简单的Controller来暴露一个端点: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Hello,Swagger!"; } } ``` 启动Spring Boot应用后,访问`http://localhost:端口号/swagger-ui.html`(端口号为你的应用启动时配置的端口,比如8080),你就可以看到Swagger UI界面,并且可以测试`/hello`端点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CrissChan

开心就好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值