huliguo
2025-07-02 692d4b07bb1186b811c2d895e5ad782c08441913
swagger
1个文件已添加
4个文件已修改
67 ■■■■■ 已修改文件
pom.xml 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/linghu/LingHuApplication.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/linghu/config/Swagger2Config.java 46 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/linghu/controller/TypeController.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -39,6 +39,19 @@
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Swagger2 核心依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- Swagger2 UI界面(可选,提供可视化文档) -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
src/main/java/com/linghu/LingHuApplication.java
@@ -10,6 +10,7 @@
    public static void main(String[] args) {
        SpringApplication.run(LingHuApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙ 启动成功   ლ(´ڡ`ლ)゙");
    }
}
src/main/java/com/linghu/config/Swagger2Config.java
New file
@@ -0,0 +1,46 @@
package com.linghu.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;
@Configuration
@EnableSwagger2  // 开启Swagger2
public class Swagger2Config {
    /**
     * 配置Swagger2核心对象Docket
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // 配置API文档基本信息
                .apiInfo(apiInfo())
                // 选择需要生成文档的接口
                .select()
                // 扫描指定包下的接口(替换为你的Controller包路径)
                .apis(RequestHandlerSelectors.basePackage("com.linghu.controller"))
                // 匹配所有路径
                .paths(PathSelectors.any())
                .build();
    }
    /**
     * 配置API文档基本信息(标题、描述、作者等)
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("灵狐GEO系统 接口文档")  // 文档标题
                .description("使用 Swagger2 生成的API文档")  // 文档描述
                .version("1.0.0")  // 版本号
                .build();
    }
}
src/main/java/com/linghu/controller/TypeController.java
@@ -5,6 +5,8 @@
import com.linghu.model.common.ResponseResult;
import com.linghu.model.entity.Type;
import com.linghu.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@@ -12,12 +14,14 @@
@RestController
@RequestMapping("/type")
@Api(value = "类型相关接口", tags = "设置-类型")
public class TypeController {
    @Autowired
    private TypeService typeService;
    @PostMapping
    @ApiOperation(value = "添加类型")
    public ResponseResult<Type> add(@RequestBody Type type) {
        type.setDelFlag(0);
        boolean success = typeService.save(type);
src/main/resources/application.yml
@@ -9,3 +9,6 @@
    username: root
    password: "123456"
    driver-class-name: com.mysql.cj.jdbc.Driver
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher  # 使用 AntPathMatcher 替代 PathPatternMatcher