利用swagger集成测试和文档

1.添加依赖



io.springfox
springfox-swagger2
2.9.2



io.springfox
springfox-swagger-ui
2.9.2



com.github.xiaoymin
swagger-bootstrap-ui
1.9.2

2.启动类配上@EnableSwagger2//启动swagger

3.类上加上描述信息的注解

@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "积分等级管理模块")
public class AdminIntegralGradeController {
@Resource
IntegralGradeService integralGradeService;

//查询所有积分等级数据
@ApiOperation("查询所有积分等级列表")
@GetMapping
public List list(){
return integralGradeService.list();
}

@ApiOperation("根据id查询积分等级")
@GetMapping("{id}")
public IntegralGrade getById(@ApiParam("积分等级id") @PathVariable("id") Long id){
return integralGradeService.getById(id);
}

@ApiModel(value="IntegralGrade对象", description="积分等级表")
public class IntegralGrade implements Serializable {

private static final long serialVersionUID = 1L;

@ApiModelProperty(value = "编号")
@TableId(value = "id", type = IdType.AUTO)
private Long id;

4.访问地址
http://localhost:8110/swagger-ui.html#
http://localhost:8110/doc.html

5.分组管理
(1)添加配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {

//一个Docker代表一 个swagger的分组:一 个分组 中可以将多个四配的Controller进行管理
@Bean
public Docket adminApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("adminApi")
.apiInfo(adminApiInfo())
.select()
//只显示admin路径下的页面
.paths(Predicates.and(PathSelectors.regex("/admin/.*")))
.build();

}

private ApiInfo adminApiInfo(){

return new ApiInfoBuilder()
.title("xxx后台管理系统-API文档")
.description("本文档描述了xxx后台管理系统接口")
.version("1.0")
.contact(new Contact("Example", "http://example.com", "example@126.com"))
.build();
}

@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
//只显示admin路径下的页面
.paths(Predicates.and(PathSelectors.regex("/api/.*")))
.build();

}

private ApiInfo webApiInfo(){

return new ApiInfoBuilder()
.title("xxx用户系统-API文档")
.description("本文档描述了xxx用户系统接口")
.version("1.0")
.contact(new Contact("Example", "http://example.com", "example@126.com"))
.build();
}}
(2)启动类中配置扫描包
@ComponentScan(basePackages = "com.example.condig")//config扫描

即会根据配置的路径过滤分组

posted @ 2025-04-19 15:13  必行之码  阅读(27)  评论(0)    收藏  举报