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

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

配置

maven

        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <scope>provided </scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <scope>provided </scope>
        </dependency>

编写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

posted @ 2022-03-08 01:30  yangruomao  阅读(249)  评论(0编辑  收藏  举报