Java教程

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

本文主要是介绍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

这篇关于Springboot 使用 Wrapper ——在线文档和测试接口的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!