Springboot中整合Swagger插件

本文详细介绍了如何在Springboot项目中整合Swagger2插件,包括pom.xml的依赖管理,Config、Controller和Pojo层的配置,以及yaml文件中针对不同环境的开关设置,以实现Swagger的自动管理。
摘要由CSDN通过智能技术生成

1.项目构成
在这里插入图片描述

2.pom.xml(3.00版本swagger-ui.html无法打开,我降级了版本)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.swagger</groupId>
    <artifactId>testswagger</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>testswagger</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.准备工作Config层、Controller层、User层、Pojo层
Config:

package com.testswagger.Config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
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;

import java.util.ArrayList;

@Configuration
@EnableSwagger2//开启swagger

public class SwaggerConfig {
    //配置swagger的Docket的bean实例,以及swagger分组,即配置多个Docket
    @Bean
    public  Docket docket2(){

        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }



    @Bean
    public Docket getDocket(Environment environment){//使用enviroment 获取当前环境,如果是特定环境开启swagger,否则不开启
        Profiles profile=Profiles.of("dev","test");
        System.out.println(profile);
        boolean flag=environment.acceptsProfiles(profile);
        System.out.println(flag);

        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())   //配置apiInfo
                .enable(flag)
                .groupName("测一测")
                .select()  //配置需要扫描的接口
                //any:所有接口和类
                //.apis(RequestHandlerSelectors.basePackage("com.testswagger.Controller")) config下的所有类
                //withClassAnnotation(RestController.class) 所有带有restController的类或接口
                //.withMethodAnnotation(GetMapping.class) 所有method=get的类或接口
                .apis(RequestHandlerSelectors.basePackage("com.testswagger.Controller"))
                //.paths(PathSelectors.ant())过滤器
                .build()

                ;
    }

    //配置apiInfo的内容
    private ApiInfo apiInfo() {
        Contact contact= new Contact("hello", "https://www.baidu.com/", "3336107384@qq.com");
        ApiInfo apiInfos=new ApiInfo(
                "hello开发的第一个Swagger",
                "加油!欧里给!!!!",
                "1.0",
                "https://www.bilibili.com/video/BV1PE411i7CV?p=48",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

        return  apiInfos;
    }
}

Controller:

package com.testswagger.Controller;

import com.testswagger.pojo.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/")
    public String toHello(){
        return "hello";
    }


    @ApiOperation("方法声明注释")
    @GetMapping("/user")
    public User toUser(){
        return  new User();
    }

}

Pojo:

package com.testswagger.pojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Repository;


@ApiModel("用户模型")
@Repository
public class User {
    //如果是string 则不公开,
    @ApiModelProperty("名字说明")
    public   String name;
    public String password;
}

4.yaml文件配置(重点解决自动根据环境开关swagger)
application.properties:

spring.profiles.active=dev

application-dev.yml:

server:
   port:8080

5.效果图
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我可以给出以下步骤: 1. 在 pom.xml 文件添加 Swagger2 和 Swagger UI 的依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> ``` 2. 创建 Swagger 配置类,用于配置 Swagger 的基本信息和扫描的包路径: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot 3 整合 Swagger") .description("使用 Swagger 构建 API 文档") .version("1.0.0") .build(); } } ``` 3. 在 Controller 类添加 Swagger 注解,用于描述接口信息: ```java @RestController @RequestMapping("/user") @Api(tags = "用户管理") public class UserController { @GetMapping("/{id}") @ApiOperation(value = "根据 ID 获取用户信息", notes = "根据传入的 ID 参数获取用户信息") @ApiImplicitParam(name = "id", value = "用户 ID", required = true, dataType = "Long", paramType = "path") public User getUserById(@PathVariable Long id) { // 根据 ID 查询用户信息 return userService.getUserById(id); } // 其他接口方法... } ``` 4. 启动应用程序,访问 http://localhost:8080/swagger-ui.html 即可查看生成的 API 文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值