前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot项目整合Swagger2

springboot项目整合Swagger2

作者头像
JavaEdge
发布2021-02-23 15:25:01
3950
发布2021-02-23 15:25:01
举报
文章被收录于专栏:JavaEdgeJavaEdge

项目整合 swagger

在聚合工程中的父 pom 工程的 pom 文件中添加依赖

代码语言:javascript
复制
<dependency>
	<groupId>io.springfoxgroupId>
	<artifactId>springfox-swagger2artifactId>
	<version>2.4.0version>
dependency>
<dependency>
	<groupId>io.springfoxgroupId>
	<artifactId>springfox-swagger-uiartifactId>
	<version>2.4.0version>
dependency>
<dependency>
	<groupId>com.github.xiaoymingroupId>
	<artifactId>swagger-bootstrap-uiartifactId>
	<version>1.6version>
dependency>
  1. 指定API类型为swagger2.0
  2. 定义API文档汇总信息
  3. 指定controller包
  4. 所有controller

源码解析

代码语言:javascript
复制
package springfox.documentation.spi;

import org.springframework.http.MediaType;
import org.springframework.plugin.metadata.SimplePluginMetadata;

public class DocumentationType extends SimplePluginMetadata {
  public static final DocumentationType SWAGGER_12 = 
  	new DocumentationType("swagger", "1.2");
  public static final DocumentationType SWAGGER_2 = 
  	new DocumentationType("swagger", "2.0");
  public static final DocumentationType SPRING_WEB = 
  	new DocumentationType("spring-web", "1.0");
  private final MediaType mediaType;

  /**
   * Creates a new instance of {@code SimplePluginMetadata}.
   */
  public DocumentationType(String name, String version, MediaType mediaType) {
    super(name, version);
    this.mediaType = mediaType;
  }

  public DocumentationType(String name, String version) {
    this(name, version, MediaType.APPLICATION_JSON);
  }

  public MediaType getMediaType() {
    return mediaType;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof DocumentationType)) {
      return false;
    }
    if (!super.equals(o)) {
      return false;
    }

    DocumentationType that = (DocumentationType) o;

    return super.equals(that) && mediaType.equals(that.mediaType);

  }

  @Override
  public int hashCode() {
    int result = super.hashCode();
    result = 31 * result + mediaType.hashCode();
    return result;
  }
}

Swagger JSON Doc

  • 在线编辑地址 https://editor.swagger.io/
  • 编辑生成样板代码

所以 swagger 不仅支持代码优先还支持契约优先编程。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-01-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 项目整合 swagger
  • 源码解析
  • Swagger JSON Doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档