Swagger初步使用
开始
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
@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();
}
}