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

Swagger配置与使用

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

Swagger配置与使用

1、导入两个依赖
 
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
        
            com.github.xiaoymin
            swagger-bootstrap-ui
            1.9.6
        

Swaggeui推荐使用第三方的依赖

2、在项目中使用java配置类的方式配置swagger
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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    

    
    @Bean
    public Docket createRestApi(){
        return new Docket(documentationType.SWAGGER_2) //指定文档风格
                .apiInfo(apiInfo())
                .select() //选择生成策略
                .apis(RequestHandlerSelectors.basePackage("com.zstudyj.controller")) //选择要生成接口文档的类
                .paths(PathSelectors.any())
                .build();
    }

    
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("xx商城接口文档") //添加标题
                .description("xx商城接口文档") //添加描述
                .contact(new Contact("zxj","http:localhost:8081/doc.html","xxxx@xxxx.com"))
                .version("v 2.0.1")
                .build();
    }

}

配置完成 ,访问接口 http://ip:port/doc.html

3、Swagger注解说明

swagger提供了一套注解, 可以对每个接口进行详细说明

  1. @Api 类注解,在控制器类添加此注解,可以对控制器类进行功能说明

    效果

  1. @ApiOperation 方法注解,在方法上添加此注解,可以对方法进行功能说明

效果

  1. @ApiImplicitParams和@ApiImplicitParam参数注解,说明参数的类型,名称,是否必须,默认值等


效果

  1. @ApiModel和@ApiModelProperty当接口参数和返回值为对象类型时,在实体类中添加注解说明

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

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

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