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

SpringBoot学习(八)——Swagger

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

SpringBoot学习(八)——Swagger

Swagger 创建springboot项目

导入依赖


    org.springframework.boot
    spring-boot-starter-web


    io.springfox
    springfox-boot-starter
    3.0.0

创建HelloController

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}
Swagger配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
Failed to start bean ‘documentationPluginsBootstrapper错误

在启动项目时报错,Failed to start bean ‘documentationPluginsBootstrapper

在application.properties中添加

spring.mvc.pathmatch.matching-strategy=ant_path_matcher
启动项目

启动项目,访问http://localhost:8080/swagger-ui/index.html

Swagger文档信息配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }

    private ApiInfo apiInfo(){
        Contact contact = new Contact("阿强", "https://blog.csdn.net/qq_41505957", "1758043090@qq.com");
        return new ApiInfo("我的API文档",
                "阿强的swagger文档",
                "v1.0",
                "https://blog.csdn.net/qq_41505957",// 自己的团队组织网站
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>()
        );

    }
}

扫描指定包下类
public Docket docket(){
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).
            select().
            apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")).
            build();
}
配置API文档的分组

使用groupName()为api文档分组。

当想创建多个分组时,需要创建多个Docket Bean

@Bean
public Docket docket(){
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).
            groupName("阿强").
            select().
            apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")).
            build();
}

@Bean
public Docket docket1(){
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).
            groupName("A");

}

@Bean
public Docket docket2(){
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).
            groupName("B");

}
@Bean
public Docket docket3(){
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).
            groupName("C");

}

Models实体类配置

创建一个实体类,@ApiModel用于实体类上

@ApiModelProperty用于实体类属性上

@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    private String userName;
    @ApiModelProperty("密码")
    private String passWord;
    public User(String userName, String passWord) {
        this.userName = userName;
        this.passWord = passWord;
    }
}

当实体类作为方法的返回值时,文档中会有实体类的说明

@GetMapping("/getUser")
public User getUser(User user){
    return user;
}

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

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

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