SpringBoot 集成 Swagger的案例

发布时间:2020-10-28 13:45:55 作者:小新
来源:亿速云 阅读:191

小编给大家分享一下SpringBoot 集成 Swagger的案例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

什么是Swagger

  1. Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API
  2. Swagger 可以生成客户端SDK代码用于各种不同的平台上的实现
  3. Swagger 文件可以在许多不同的平台上从代码注释中自动生成
  4. Swagger 有一个强大的社区

依赖导入

<!-- Swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.4.0</version>
</dependency>

加入配置

swagger:
  title: 项目 API
  description: SpringBoot 集成 Swagger 项目 API
  version: 1.0
  terms-of-service-url: http://www.baidu.com/
  base-package: cn.anothertale.springbootshiro  # 这一项指定需要生成 API 的包,一般就是 Controller
  contact:
    name: taohan
    url: http://www.baidu.ccom/
    email: 1289747698@qq.com

建立 Swagger Config

package cn.anothertale.springbootshiro.config.swagger;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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;

/**
 * description: swagger 配置中心
 *
 * @author: taohan
 * @date: 2019年03月20日
 * @time: 16:52
 */
@Getter
@Setter
@Configuration
@EnableSwagger2
@ConditionalOnClass(EnableSwagger2.class)
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfig {

    /**
     * API 接口包路径
     */
    private String basePackage;

    /**
     * API 页面标题
     */
    private String title;

    /**
     * API 描述
     */
    private String description;

    /**
     * 服务条款地址
     */
    private String termsOfServiceUrl;

    /**
     * 版本号
     */
    private String version;

    /**
     * 联系人
     */
    private Contact contact;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .version(version)
                .contact(contact)
                .build();
    }
}

通过注解标明 API

Swagger 默认根据配置的包,扫描所有接口并生成对应的 API 描述和参数信息。

常用注解及对应属性如下:

最后,可以在浏览器中输入 http://localhost:8080/swagger-ui.html 即可访问!

以上是SpringBoot 集成 Swagger的案例的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. springboot集成swagger-UI 开发API项目
  2. springboot中如何集成swagger

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

springboot bo wa

上一篇:AtomicInteger中decrementAndGet方法是什么

下一篇:springboot 2.0.8 跳转jsp页面的示例

相关阅读

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

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