Spring 集成 Swagger

Swagger 是一个规范且完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务

Swagger 的目标是对 REST API 定义一个标准且和语言无关的接口,可以让人和计算机拥有无须访问源码、文档或网络流量监测就可以发现和理解服务的能力。当通过 Swagger 进行正确定义,用户可以理解远程服务并使用最少实现逻辑与远程服务进行交互。与为底层编程所实现的接口类似,Swagger 消除了调用服务时可能会有的猜测

Swagger 的优势

  支持 API 自动生成同步的在线文档:使用 Swagger 后可以直接通过代码生成文档,不再需要自己手动编写接口文档了,对程序员来说非常方便,可以节约写文档的时间去学习新技术

  提供 Web 页面在线测试 API:光有文档还不够,Swagger 生成的文档还支持在线测试。参数和格式都定好了,直接在界面上输入参数对应的值即可在线测试接口

现有项目使用的是spring框架 现需要集成Swagger,步骤如下:

  1.添加依赖

<dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>1.5.21</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-models</artifactId>
        <version>1.5.21</version>
    </dependency>
    <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>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

  2.创建 Swagger 配置类

package com.swagger.config;
import io.swagger.annotations.Api; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableWebMvc @Configuration @EnableSwagger2 public class SwaggerConfig {
//Swagger 开关 配置在配置文件中 测试环境打开 生产环境关闭 @Value(value = "${swagger.enabled:false}") private Boolean swaggerEnabled; @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) // 是否开启 .enable(swaggerEnabled) .select() //只显示添加@Api注解的类 .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("XXX系统") .description("接口文档") .version("1.0.0") .build(); } }

  3.在spring.xml 增加扫描配置

 <!-- 扫描swagger包路径 -->
    <context:component-scan  base-package="com.swagger.config"/>

  4.后续就可以在controller和entity中 增加注解生成API文档

@RestController
@RequestMapping(value = { "/commodity/" })
// 类上加@Api注解
@Api(value = "/CommodityController", tags = "商品查询接口")
public class CommodityController{
@RequestMapping(value = "/{id}", method = RequestMethod.GET) // 方法上加ApiOpreation注解 @ApiOperation(value = "根据id获取产品信息", notes = "根据id获取商品信息", httpMethod = "GET", response = Commodity.class) public ResponseEntity<Commodity> get(@PathVariable Long id) {
Commodity commodity= new Commodity();
commodity.setName("T恤");
commodity.setId(1L);
return ResponseEntity.ok(commodity);
} }
package com.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;

@ApiModel(value = "商品列表查询参数")
@Data
public class Commodity {

    @ApiModelProperty(value = "商品名称")
    private String name;

    @ApiModelProperty(value = "商品 id")
    private long id;
}

  5.在配置文件中配置开关:swagger.enabled=true  测试环境打开 生产环境关闭

就可以启动项目 访问:http://localhost:8080/项目名/swagger-ui.html  查看api

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Spring Gateway 中集成 Swagger,你可以按照以下步骤进行操作: 1. 首先,确保你已经在你的 Spring Boot 项目中集成Swagger。你可以使用 `springfox-swagger2` 和 `springfox-swagger-ui` 依赖来实现这一点。在你的 `build.gradle` 或 `pom.xml` 文件中添加以下依赖: ``` implementation 'io.springfox:springfox-swagger2:2.9.2' implementation 'io.springfox:springfox-swagger-ui:2.9.2' ``` 2. 创建一个 Swagger 配置类,于配置 Swagger 相关的配置。例如: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } } ``` 上述示例中,我们创建了一个名为 `api()` 的 `Docket` Bean,并配置了要扫描的 API 包路径。 3. 在你的 Gateway 配置类中添加 Swagger 相关的路由规则。例如: ```java @Configuration public class GatewayConfig { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() // Swagger UI 路由规则 .route("swagger_ui_route", r -> r.path("/swagger-ui.html") .uri("classpath:/META-INF/resources/swagger-ui.html")) .route("swagger_ui_assets", r -> r.path("/webjars/**") .uri("classpath:/META-INF/resources/webjars/")) // Swagger API 路由规则 .route("swagger_api_route", r -> r.path("/v2/api-docs") .filters(f -> f.rewritePath("/v2/api-docs", "/v2/api-docs")) .uri("lb://your-service-name")) .build(); } } ``` 上述示例中,我们创建了两个路由规则,一个用于 Swagger UI,另一个用于 Swagger API。请将 `your-service-name` 替换为你的实际服务名称。 4. 运行你的应用程序,并访问 http://localhost:8080/swagger-ui.html,你将看到 Swagger UI 页面。你可以在这里查看和测试你的 API 文档。 这就是在 Spring Gateway 中集成 Swagger 的基本步骤。通过配置 Swagger 相关的路由规则,你可以将 Swagger UI 和 Swagger API 集成到你的网关中,以便于统一管理和访问 API 文档。 希望这个步骤可以帮助你集成 SwaggerSpring Gateway!如果你有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值