开源框架-SpringBoot系列javaJAVA

Spring Boot集成Knife4j

2020-07-11  本文已影响0人  上杉丶零

一、介绍

Knife4j【快速开始】是为Java MVC框架集成Swagger生成API文档的增强解决方案(在非Java项目中也提供了前端UI的增强解决方案),前身是Swagger2,取名Knife4j是希望她能像一把匕首一样小巧、轻量,并且功能强悍。

Knife4j

二、开源仓库

https://github.com/xiaoymin/swagger-bootstrap-ui
https://gitee.com/xiaoym/knife4j

三、功能特性

核心功能

该UI增强包主要包括两大核心功能:文档说明在线调试

UI增强

同时,swagger-bootstrap-ui在满足以上功能的同时,还提供了文档的增强功能,每一个增强的功能都是贴合实际,考虑到开发者的实际开发需要,是必不可少的功能,主要包括:

四、功能预览

<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-boot-starter -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.4</version>
</dependency>
package game.leif.fantasy_all_star;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("game.leif.fantasy_all_star.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Fantasy All Star - 接口文档")
                .description("本项目模拟了回合制对战类游戏,让玩家能够体验到来自不同游戏、动漫、影视、小说中的经典人物互相组队对战的畅爽快感。")
                .contact(new Contact("Leif Liu", null, "leif1995@dingtalk.com"))
                .version("v1.0.0")
                .build();
    }
}
filterURLSet.add("/swagger-resources");
filterURLSet.add("/v2/api-docs");
filterURLSet.add("/v2/api-docs-ext");
#Knife4j
location ^~ /knife4j {
    proxy_pass http://192.168.2.192:20300/api/doc.html;
}
location ^~ /webjars/ {
    proxy_pass http://192.168.2.192:20300/api/webjars/;
}
location ^~ /swagger-resources {
    proxy_pass http://192.168.2.192:20300/api/swagger-resources;
}
location ^~ /v2/api-docs {
    proxy_pass http://192.168.2.192:20300/api/v2/api-docs;
}
@Api(value = "用户", tags = {"用户"})
@ApiOperation(value = "登陆接口", notes = "用于用户登陆")
@ApiImplicitParams({
        @ApiImplicitParam(name = "response", value = "参数用例", required = false, dataType = DataTypeConstants.STRING, paramType = RequestTypeConstants.QUERY)
})
@ApiResponses({
        @ApiResponse(code = 1, message = "自定义响应状态码")
})
@ApiOperationSupport(order = 1, params = @DynamicParameters(name = "CreateOrderModel",properties = {
        @DynamicParameter(name = "id",value = "注解id",example = "X000111",required = true,dataTypeClass = Integer.class),
        @DynamicParameter(name = "name",value = "订单编号",required = false)
}), responses = @DynamicResponseParameters(properties = {
        @DynamicParameter(name = "result", value = "返回结果", dataTypeClass = String.class)
}))
@ApiModel(description = "登陆表单")
@ApiModelProperty(value = "用户名", required = true, example = "leif", position = 1)
# 是否停用Knife4j文档
knife4j:
  production: false
接口文档
在线调试
全局参数设置
离线文档
个性化设置
上一篇下一篇

猜你喜欢

热点阅读