基于yaml格式的swagger文档的编写(常应用于微服务中)

一. 引入maven依赖


       <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>

二 .编写swagger-api.yml

swagger: "2.0"
info:
  title: "使用swagger-ui文档"
  description: "使用swagger-ui文档"
  version: "1.1.0"
basePath: "/test-service"
tags:
- name: "tag1"
- name: "tag2"
- name: "tag2"
schemes:
- "http"
paths:
  /test/queryUser:
    post:
      tags:
      - "tag1"
      summary: "查询用户(用于测试)"
      description: ""
      operationId: ""
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
       - in: "query"
         name: "userName"
         type: string
         description: "用户名称"
         required: true
      responses:
        200:
          description:
          schema:
            $ref: "bean-definition.yml#/definitions/resultDto"
        400:
          description: Bad request
        401:
          description: Unauthorized
        403:
          description: Forbidden
        404:
          description: Not found
        500:
          description: Internal Server Error
  /test/modifyUser:
    post:
      tags:
      - "tag2"
      summary: "修改用户信息(用于测试)"
      description: ""
      operationId: ""
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
        - in: "body"
          name: "body"
          description: "修改用户信息入参Json"
          required: true
          schema:
            $ref: "bean-definition.yml#/definitions/modifyParameter"
      responses:
        200:
          description:
          schema:
            $ref: "bean-definition.yml#/definitions/resultDto"
        400:
          description: Bad request
        401:
          description: Unauthorized
        403:
          description: Forbidden
        404:
          description: Not found
        500:
          description: Internal Server Error

三.编写bean-definition.yam文件

definitions:
  modifyParameter:
    type: "object"
    properties:
      userId:
        type: "string"
        description: "用户id"
        default: "testId"
      UserName:
        type: "string"
        description: "用户名称"
        default: "userName"
  
  ResultDto:
      type: "object"
      properties:
        code:
          type: "string"
          default: "000000"
        message:
          type: "string"
          default: "成功"
        data:
          type: "object"
 

四. 创建swagger配置类


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import springfox.documentation.swagger.web.InMemorySwaggerResourcesProvider;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    @Primary
    public SwaggerResourcesProvider swaggerResourcesProvider(InMemorySwaggerResourcesProvider defaultResourcesProvider) {
        return () -> {
            SwaggerResource resource = new SwaggerResource();
            resource.setName("swagger使用 API");
            resource.setSwaggerVersion("2.0");
            resource.setLocation("/swagger-api.yml");
            List<SwaggerResource> resourcesList = new ArrayList<>(defaultResourcesProvider.get());
            resourcesList.clear();
            resourcesList.add(0, resource);
            return resourcesList;
        };
    }

}



五. 注意事项

由于yaml格式的文本,采用缩进的方式进行层级的划分,故在编写时极其容易出现错误,下面推荐两个可以对yaml格式校验的网站便于排除在编写yaml格式的文件的错误。

yaml校验工具(使用上感觉上面的这个网址提示较完善))
http://www.bejson.com/validators/yaml_editor/

editor.swagger.io/#

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值