Knife使用说明(Swagger2)

访问地址

http://$ {host}:$ {port}/doc.html 

@Api(tags ="xxx管理") 标注类作用

备注:一般此注解加在控制层。加在类上:标注此类是xxx管理

@ApiOperation("xxx接口") 标注接口作用

配合Api使用,标注接口作用

编写Swagger2Config配置文件

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.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * @author xsm
 * @create 2023-07-26-16:20
 */
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    @Bean
    public Docket createTestApi() {// 创建API基本信息
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 对所有api进行监控
                .apis(RequestHandlerSelectors.any())
                //不显示错误的接口地址
                .paths(PathSelectors.regex("/error.*").negate())//错误路径不监控
                //扫描所有的包 可以扫描指定的包 .apis(RequestHandlerSelectors.basePackage("具体controller所在的包"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 创建API的基本信息,这些信息会在Swagger UI中进行显示
     * @return API的基本信息
     */
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                //标题
                .title("kle-track-server")
                // API描述
                .description("Swagger-接口文档")
                //接口的版本
                .version("1.0.0")
                //作者
                .contact("xsm")
                .build();
    }
}

配置内容的显示位置

注意事项:

以下为网友遇到问题,非本人项目问题。

记录保存,遇到解决。

@Configuration
public class IntercpetorConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 设置swagger静态资源访问
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
/swagger-ui.html,/swagger-resources/**,/csrf,/webjars/**
若项目中之前使用过 swagger 生成接口文档,改成 knife 其实很简单,只需要两步:
pom.xml 中把 swagger-ui jar包替换成 knife4j-spring-ui
访问地址由原来的 http://${host}:${port}/swagger-ui.html 改成 http://${host}:${port}/doc.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值