Springboot 使用 Wrapper ——在线文档和测试接口


Wrapper 可以生成一个在线的文档,并且可以在里面方便的进行Rest风格测试

配置

maven

        
        
            io.springfox
            springfox-swagger2
            provided 
        
        
            io.springfox
            springfox-swagger-ui
            provided 
        

编写config配置类

直接拷贝,改关键信息即可,该类要被springboot扫描到

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("java", "http://acdongla.cc", "wyiheyes@163.com"))
                .build();
    }
}

使用

浏览器进入/swagger-ui.htm进入界面

image-20220308011728105

定义更易读的API文档

使用注解即可

@Api(description = "讲师管理") 定义在类上

@ApiOperation("查询所有讲师信息") 定义在方法上

@ApiParam(name = "id", value = "讲师id", required = true) 定义在参数上

image-20220308012823886

效果如图

image-20220308012854335