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

swagger文档配置

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

swagger文档配置

swagger:

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

springboot整合swagger 添加依赖

有两种模式,一个是swagger-ui.html,一个是doc.html(按自己要求所取)

        
            io.springfox
            springfox-swagger2
            2.9.2
        

        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        

        
        
            com.github.xiaoymin
            swagger-bootstrap-ui
            1.9.2
        
 创建swagger配置类:
package com.yan.config;

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration // 告诉springboot需要加载这个配置类
@EnableSwagger2 // 启用swagger
public class SwaggerConfig {
    
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 通过select返回一个实例,用来控制哪些接口暴露给swagger来展示。
                .select()
                // 对所有api进行监控
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }


    // 创建api的基本信息,(这些信息会展示在文档中,比如title描述之类的)
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("spring-web项目")
                .description("对项目的描述").build();
    }
}
验证:

项目启动之后,可以在访问localhost:8080/swagger-ui.html或者localhost:8080/doc.html来访问了。(根据自己依赖导入来选择)

一些注解:
Controller层

定义接口名称:@Api(tags="xxx")
定义方法名称:@ApiOperation(value="xxx")
定义param参数的各个属性:@ApiImplicitParam(name = "xx", value = "xx", paramType = "xx")
如: @ApiImplicitParam(name = "pid", value = "产品id", paramType = "String")

实体
定义对象名称:@ApiModel("xxx")
定义参数名称:@ApiModelProperty(value = "xxx")

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

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

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