SpringBoot使用knife4j进行在线接口调试

SpringBoot使用knife4j进行在线接口调试

前言

我们在开发一个Java Web的项目,如果项目整体采用前后端分离的架构的方式,我们会经常使用Swagger来进行接口调试和为前端提供接口文档,但是Swagger并没有实际上那么方便,比如我们在发送Post请求时,参数选填还是非常不友好,那么有没有更好的工具呢?

正文

knife4j

knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案,前身是swagger-bootstrap-ui,具有小巧,轻量,并且功能强悍的优点。

Knife4j提供两大核心功能:文档说明 和 在线调试

  • 文档说明:根据Swagger的规范说明,详细列出接口文档的说明,包括接口地址、类型、请求示例、请求参数、响应示例、响应参数、响应码等信息,使用swagger-bootstrap-ui能根据该文档说明,对该接口的使用情况一目了然。
  • 在线调试:提供在线接口联调的强大功能,自动解析当前接口参数,同时包含表单验证,调用参数可返回接口响应内容、headersCurl请求命令实例、响应时间、响应状态码等信息,帮助开发者在线调试,而不必通过其他测试工具测试接口是否正确,简洁、强大。

SpringBoot使用knife4j进行在线接口调试

注入依赖

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.4</version>
</dependency>

SwaggerConfig.class :knife4j配置类

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {


    /**
     * 这里配置swagger扫描的包
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("com.luo.producer"))
                .paths(PathSelectors.any()).build();
    }


    /**
     * 这里配置swagger对外提供服务的端口
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("发布模拟boos接口")
                .description("简单优雅的发布模拟boos接口restful风格接口")
                // .termsOfServiceUrl("http://127.0.0.1:8080/doc.html")
                .version("1.0").build();
    }
}

验证

测试接口

@RestController
@Slf4j
public class UserController {
    @GetMapping("/helloword")
    public String hello(String input){
        return "你好,"+input;
    }
 }

启动项目后:访问http://127.0.0.1:8080/doc.html

在这里插入图片描述

访问测试接口,进行测试:

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_40990818/article/details/108427053