栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

swagger2简单生成文档

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

swagger2简单生成文档

☔内容介绍☔

swagger2所需要的依赖,swagger2的特点,swagger2注解介绍


         本文目录

1 swagger2简单介绍

1.2 特点

2 配置依赖

3 配置并使用

3.1 配置类

3.2 启动类注解

3.3 接口添加注解

4 测试


1 swagger2简单介绍

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务

1.2 特点

及时性 (接口变更后,能够及时准确地通知相关前后端开发人员)
规范性 (并且保证接口的规范性,如接口的地址,请求方式,参数及响应格式和错误信息)
一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)
可测性 (直接在接口文档上进行测试,以方便理解业务

2 配置依赖


   io.springfox
   springfox-swagger-ui
   2.9.2


   io.springfox
   springfox-swagger2
   2.9.2

3 配置并使用

3.1 配置类
package com.salsl.swagger2.utils;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

// 配置类
@Configuration
// 开启Swagger2
@EnableSwagger2
// 配置生产环境下不可用 dev(开发) test(测试) prod(生产)
@Profile({"dev","test"})
public class Swagger2Configuration extends WebMvcConfigurationSupport {

    // api接口包扫描路径
    public static final String
            SWAGGER_SCAN_BASE_PACKAGE = "com.salsl.swagger2.controller";
    // 指定当前Swagger API文档版本
    public static final String VERSION = "1.0.0";

    
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 接口文档的基本信息
                .apiInfo(apiInfo())
                .select()
                // 方法需要有ApiOperation注解才能生存接口文档
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 路径使用any风格
                .paths(PathSelectors.any())
                .build();
    }

    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot中使用Swagger2构建RestFul APIs")
                .description("测试系统")
                // .termsOfServiceUrl("http://www.**.com")
                .version(VERSION)
                .build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

3.2 启动类注解

@EnableSwagger2

3.3 接口添加注解

3.3.1 类上面添加

@Api(value = "测试显示" description = "书本管理接口")

3.3.2 方法上

@ApiOperation(value = "新增书本")

4 测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/915229.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号