• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot 集成 Swagger的方法介绍(附代码)

springboot 搞代码 4年前 (2022-01-09) 14次浏览 已收录 0个评论

本篇文章给大家带来的内容是关于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: [email protected]

建立 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();    }}

通过注本文来源gaodai#ma#com搞*!代#%^码$网*解标明 API

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

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

  • @Api (描述一个 API 类,标注在 Controller 上)

    1. value:url 的路径值
    2. tags:如果设置该值,value 的值会被覆盖
    3. description:API 的资源描述
    4. basePath:基本路径可以不设置
    5. produces:比如:application/json, application/xml 类似 RequestMapping 对应属性
    6. consumes:比如:application/json, application/xml
    7. authorizations:高级特性认证时配置
    8. hidden:是否在文档中隐藏

  • 搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
    转载请注明原文链接:SpringBoot 集成 Swagger的方法介绍(附代码)

    喜欢 (0)
    [搞代码]
    分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址