SpringBoot学习(八)——Swagger

78 篇文章 0 订阅
10 篇文章 0 订阅

Swagger

创建springboot项目

导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

创建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;
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值