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

SpringBoot项目中Swagger的使用

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

SpringBoot项目中Swagger的使用

1.首先在pom中项目中导入Swagger的依赖:(需要两个依赖,一个springfox-swagger2,一个springfox-swagger-ui)



    io.springfox
    springfox-swagger2
    2.9.2


    io.springfox
    springfox-swagger-ui
    2.9.2

2.导入依赖后,再写一个Swagger配置类开启Swagger:(配置类中什么都不写就是默认的,也可以自定义配置)

@Configuration
@EnableSwagger2
public class SwaggerConfig {

}

@Configuration表明这是一个注解类,@EnableSwagger2表示开启Swagger2。也可以在启动类上加上@EnableSwagger2开启注解。

3.在浏览器输入网址到Swagger页面:http://localhost:8080/swagger-ui.html ,如果控制层注入容器即添加有@RestController。就自动可以识别接口并显示到文档中。

补充:自定义文档信息

@Configuration
public class SwaggerConfig {

    

    @Bean
    public Docket creatRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ape.hospital_order.controller"))
                .paths(PathSelectors.any()) //定义哪些路径的接口需要生成文档
                .build();
    }

    
    private ApiInfo apiInfo(){
        Contact contact = new Contact("用户名", "www.yonghu.cn", "12345678@qq.com");
        return new ApiInfoBuilder()
                .title("xxx系统") //文档首页标题
                .version("1.0") // 文档版本
                .description("xxx系统描述")
                .contact(contact) // 创建者信息
                .build();
    }

}

4.在类或方法上添加注解,对类和接口说明:

@RestController
@RequestMapping("/checkitem")
@Api(tags = "业务的说明",value = "/路径")
public class CheckItemController {



    @PostMapping("/find")
    @ApiOperation(value = "查找页面", notes = "分页查询")
    public One find(@RequestBody Query query){
       
        return One;
    }

    //检查项的添加
    @PostMapping("/save")
    @ApiOperation(value = "项目的添加", notes = "添加")
    public Two save(@RequestBody Add add){
         
        return Two;
    }
}


 

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

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

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