温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot集成Swagger如何添加maven依赖

发布时间:2021-03-22 11:17:07 来源:亿速云 阅读:1844 作者:小新 栏目:开发技术

这篇文章主要介绍了SpringBoot集成Swagger如何添加maven依赖,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

SpringBoot集成Swagger 添加maven依赖

<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>

要求:jdk 1.8 + 否则swagger2无法运行 要使用Swagger,我们需要编写一个配置类-SwaggerConfig来配置 Swagger

package com.yf.exam.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

  //配置了swagger的Docket 的 bean 实例
  @Bean
  public Docket docket(){
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo());
  }
  //配置 swagger 信息 = apiInfo
  private ApiInfo apiInfo(){
    //作者信息
    Contact contact = new Contact("潇七", "https://www.xhost.vip/", "2278023068@qq.com");
    return new ApiInfo("API文档",
        "接口信息",
        "v1.0",
        
        contact,
        "Apache 2.0",
        "http://www.apache.org/licenses/LICENSE-2.0",
        new ArrayList()
    );
  }
}

访问测试 :http://localhost:8080/swagger-ui.html ,可以看到swagger的界面;
-

SpringBoot集成Swagger如何添加maven依赖

knife4j

官网参考地址:knife4j
knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案(在非Java项目中也提供了前端UI的增强解决方案),前身是swagger-bootstrap-ui,取名knife4j是希望她能像一把匕首一样小巧,轻量,并且功能强悍!

简洁

基于左右菜单式的布局方式,是更符合国人的操作习惯吧.文档更清晰…

个性化配置

个性化配置项,支持接口地址、接口description属性、UI增强等个性化配置功能…

增强

接口排序、Swagger资源保护、导出Markdown、参数缓存众多强大功能.

SpringBoot集成Knife4j 添加maven依赖

<dependency>
      <groupId>com.github.xiaoymin</groupId>
      <artifactId>knife4j-spring-boot-starter</artifactId>
      <!--在引用时请在maven中央仓库搜索最新版本号-->
      <version>2.0.4</version>
 </dependency>

-访问测试 :http://localhost:8080/doc.html ,可以看到knife4j的界面;

SpringBoot集成Swagger如何添加maven依赖

离线文档导出

Knife4j提供导出4种格式的离线文档(Html\Markdown\Word\Pdf)

感谢你能够认真阅读完这篇文章,希望小编分享的“SpringBoot集成Swagger如何添加maven依赖”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI