Swagger初步使用

Swagger初步使用

开始

  • 引入依赖
  • springboot 2.6以上要用这个
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
  • 开启swagger@EnableSwagger2
@SpringBootApplication(scanBasePackages = "top.huanyv")
@EnableSwagger2
public class WebApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }
}

使用

Controller配置

@RestController
@RequestMapping("/article")
@Api(tags = "文章", description = "文章相关接口")
public class ArticleController {
}

接口参数配置

@GetMapping("/articleList")
@ApiOperation(value = "文章列表", notes="获取一页文章")
@ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "当前页码"),
        @ApiImplicitParam(name = "pageSize", value = "页长"),
        @ApiImplicitParam(name = "categoryId", value = "分类id"),
})
public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
    return articleService.articleList(pageNum, pageSize, categoryId);
}

实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "添加评论实体类")
public class AddCommentDto {
    private Long id;
    private String type;
    @ApiModelProperty(value = "文章ID")
    private Long articleId;
    
    @ApiModelProperty("根评论ID")
    private Long rootId;
    
    @ApiModelProperty("评论内容")
    private String content;
    
    private Long toCommentUserId;
    private Long toCommentId;
    private Long createBy;
    private Date createTime;
    private Long updateBy;
    private Date updateTime;
    private Integer delFlag;
}

文档信息配置

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket customDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .build();
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("团队名", "http://www.my.com", "my@my.com");
        return new ApiInfoBuilder()
                .title("文档标题")
                .description("文档描述")
                .contact(contact)   // 联系方式
                .version("1.1.0")  // 版本
                .build();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值