SpringBoot整合Swagger3.0

本文详细介绍了如何在Spring Boot项目中集成Swagger 3.0,包括添加依赖、配置Swagger环境、创建用户实体和Controller,以及生成文档页面。重点展示了如何配置不同环境下的API文档分组和API信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.引入依赖
maven仓库:https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

2、集成swagger

package com.example.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.oas.annotations.EnableOpenApi;

@Configuration
//@EnableSwagger2   //swagger2 注解
@EnableOpenApi  //swagger3 新增注解
public class SwaggerConfig {

}

3.配置swagger

@Bean
public Docket docket(Environment environment) {
    //设置要显示swagger的环境
    Profiles of = Profiles.of("dev", "test");
    //判断当前是否处于该环境
    boolean b = environment.acceptsProfiles(of);

    return new Docket(DocumentationType.OAS_30)
            .apiInfo(apiInfo())
            .enable(b)
            .groupName("group")   // 配置分组
            //是否启用swagger
            .select()   //通过select()方法去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
            //basePackage根据包路径扫描接口
            .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
            //.paths(PathSelectors.ant("/v1/**"))   //通过paths()方法设置如何过滤
            .build();
}

//配置多个分组
@Bean
public Docket docket1() {
    return new Docket(DocumentationType.OAS_30).groupName("group1");
}

@Bean
public Docket docket2() {
    return new Docket(DocumentationType.OAS_30).groupName("group2");
}

//配置文档信息
private ApiInfo apiInfo() {
    Contact contact = new Contact("联系人", "联系人连接", "联系人邮件@qq.com");
    return new ApiInfo("Swagger学习",
            "演示如何配置swagger",
            "v1.0",
            "组织连接",
            contact,
            "Apache 2.0 许可",
            "许可连接",
            new ArrayList()
    );
}

4.创建用户实体类

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel	//只要实体在请求接口的返回值上(即使是泛型),都能映射到实体项中,该注解只是起解释作用
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

5.修改controller

package com.example.controller;

import com.example.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "模块说明")
@RestController
public class HelloController {

    @ApiOperation("接口说明")
    @GetMapping("/hello")
    public String hello(@ApiParam("用户名") String username) {
        return username;
    }

    @ApiOperation("登录")
    @GetMapping("/getUser")
    public User getUser() {
        return new User();
    }

}

6.文档页面地址
http://localhost:8080/swagger-ui/index.html
以上。

你好!要将Spring Boot 2.6与Swagger 3.0整合在一起,你可以按照以下步骤进行操作: 步骤1:添加Swagger依赖 在你的Spring Boot项目的pom.xml文件中,添加Swagger的依赖: ```xml <dependencies> <!-- 其他依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> </dependencies> ``` 步骤2:配置Swagger 创建一个Swagger配置类,用于配置Swagger的相关信息: ```java import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; @Configuration @EnableSwagger2 public class SwaggerConfig { public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API 文档") .description("API 文档") .version("1.0.0") .build(); } } ``` 步骤3:启用Swagger 在你的Spring Boot应用程序的主类上使用`@EnableSwagger2`注解来启用Swagger: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Import; @SpringBootApplication @Import(SwaggerConfig.class) public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 步骤4:访问Swagger UI 在启动你的应用程序后,你可以通过访问`http://localhost:8080/swagger-ui/`来查看生成的API文档。 以上就是将Spring Boot 2.6与Swagger 3.0整合的基本步骤。你可以根据自己的需要进一步定制和配置Swagger。希望能对你有所帮助!如果有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值