springboot整合knfi4j

1.pom文件添加依赖

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

2.配置knfi4j

@Configuration
@EnableSwagger2
@EnableWebMvc
public class Knife4jConfig {
    
    @Bean
    public Docket adminApiConfig(){
        
        Docket adminApi = new Docket(DocumentationType.SWAGGER_2)
                .enable(true)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                //扫描哪个包
                .apis(RequestHandlerSelectors.basePackage("org.test.api"))
                //哪个请求路径
                .paths(PathSelectors.regex("/admin/.*"))
                .build();
        return adminApi;
    }
    
    private ApiInfo adminApiInfo(){
        return new ApiInfoBuilder()
                .title("后台管理系统-API文档")
                .description("本文档描述了后台管理系统微服务接口定义")
                .version("1.0")
                .contact(new Contact("张三","http://test.com","test@test.com"))
                .build();
        
    }
    
}


3.还要配置WebMvcConfig,不然请求doc.html会404

@Configuration
public class WebAppMvcConfig implements WebMvcConfigurer {
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
    
        /** 公共部分内容 */
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }
}

4.在controller上配置
@Api(tags="角色管理")

5.在方法上配置
@ApiOperation("查询所有角色")

6.启动项目,在项目后加 doc.html,比如http://localhost:8080/doc.html,显示如下界面代表成功

posted @ 2024-05-03 02:59  猝死的路上  阅读(74)  评论(0编辑  收藏  举报