Administrator
Administrator
Published on 2025-01-06 / 0 Visits
0
0

Springboot2集成Knife4j

Knife4j是一个集Swagger2 和 OpenAPI3为一体的增强解决方案

第一步
    <dependency>
         <groupId>com.github.xiaoymin</groupId>
         <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
         <version>4.4.0</version>
   </dependency>


第二步

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Bean(value = "dockerBean")
    public Docket dockerBean() {
        //指定使用Swagger2规范
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //描述字段支持Markdown语法
                        .description("塔里木大学XX项目接口文档")
                        .termsOfServiceUrl("http://songzhantao.cn")
                        .contact("lanqiao@qq.com")
                        .version("1.0")
                        .build())
                //分组名称
                .groupName("xxx")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("org.lanqiao.taru.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
}
第三步
@Api(tags = "首页模块")
@RestController
public class IndexController {

    @ApiImplicitParam(name = "name",value = "姓名",required = true)
    @ApiOperation(value = "向客人问好")
    @GetMapping("/sayHi")
    public Result<String> sayHi(@RequestParam(value = "name")String name){
        return Result.ok("Hi:"+name);
    }
}
第四步

万事俱备,启动Spring Boot项目,浏览器访问Knife4j的文档地址即可查看效果

http://localhost:3000/doc.html

Description


Comment