swagger+knife4j整合

swagger

pom配置

xml 复制代码
      <!-- swagger 接口文档 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

config

建立一个config文件夹,建立一个SwaggerConfig文件

java 复制代码
package com.castle.usercenter.config;

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

/**
 * 自定义Swagger接口文档的配置
 */
@Configuration
@EnableSwagger2  // 开启swagger2的自动配置
public class SwaggerConfig {
    @Bean
    public Docket docket() {
        // 创建一个 swagger 的 bean 实例
        return new Docket(DocumentationType.SWAGGER_2)
                .select()  // 设置扫描接口
                // 配置如何扫描接口
                .apis(RequestHandlerSelectors
//                        .any()  // 扫描全部借口,默认
//                        .none   // 全部不扫描
                        .basePackage("com.castle.usercenter.controller")
                        //.withClassAnnotation(RestController.class) // 扫描带有指定注解的类下所有接口
                        //.withMethodAnnotation(PostMapping.class) // 扫描带有只当注解的方法接口
                )
                .paths(PathSelectors
                        .any()  // 满足条件的路径,该断言总为true
                        //.none() // 不满足条件的路径,该断言总为false(可用于生成环境屏蔽 swagger)
                        //.ant("/user/**") // 满足字符串表达式路径
                        //.regex("") // 符合正则的路径
                ).build();
    }
    private ApiInfo apiInfo() {
        Contact contact = new Contact(
                "castleMyZ", // 作者姓名
                "z2code.site",
                "xxx@qq.com");
        return new ApiInfoBuilder()
                .title("伙伴系统-接口文档") // 标题
                .description("就要冷着") // 描述
                .termsOfServiceUrl("https://www.baidu.com") // 跳转连接
                .version("1.0") // 版本
                .license("Swagger-的使用(详细教程)")
                .licenseUrl("https://blog.csdn.net/m0_51547272")
                .contact(contact)
                .build();
    }
}

这个链接http://localhost:8080/api/swagger-ui.html#/

yml配置

yml 复制代码
mvc:  
    pathmatch:  
        matching-strategy: ant_path_matcher

Knife4j

pom文件

xml 复制代码
        <!-- knife4j 接口文档 -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.7</version>
        </dependency>

SwaggerConfig文件

加上这个@Profile({"dev", "test"}) // 版本控制访问

yml

加上这串代码

yml 复制代码
profiles:  
    active: dev

这个网站http://localhost:8080/api/doc.html#/home

相关推荐
程序新视界35 分钟前
为什么不建议基于Multi-Agent来构建Agent工程?
人工智能·后端·agent
Victor3561 小时前
Hibernate(29)什么是Hibernate的连接池?
后端
Victor3561 小时前
Hibernate(30)Hibernate的Named Query是什么?
后端
源代码•宸1 小时前
GoLang八股(Go语言基础)
开发语言·后端·golang·map·defer·recover·panic
czlczl200209251 小时前
OAuth 2.0 解析:后端开发者视角的原理与流程讲解
java·spring boot·后端
颜淡慕潇1 小时前
Spring Boot 3.3.x、3.4.x、3.5.x 深度对比与演进分析
java·后端·架构
布列瑟农的星空1 小时前
WebAssembly入门(一)——Emscripten
前端·后端
小突突突3 小时前
Spring框架中的单例bean是线程安全的吗?
java·后端·spring
iso少年3 小时前
Go 语言并发编程核心与用法
开发语言·后端·golang
掘金码甲哥3 小时前
云原生算力平台的架构解读
后端