Spring Boot 3项目使用Swagger3教程

Spring Boot 3项目使用Swagger3教程

Swagger:自动生成接口文档

添加依赖(pom.xml)

xml 复制代码
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
    <version>4.1.0</version>
</dependency>

配置Swagger

Spring Boot项目中创建一个配置类SwaggerConfig,并添加Swagger的配置信息。

java 复制代码
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SwaggerConfig {
    @Bean
    public OpenAPI springShopOpenAPI() {
        return new OpenAPI()
                .info(new Info().title("标题")
                        .contact(new Contact())
                        .description("我的API文档")
                        .version("v1")
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")))
                .externalDocs(new ExternalDocumentation()
                        .description("外部文档")
                        .url("https://springshop.wiki.github.org/docs"));
    }
}

接口注释

java 复制代码
import io.swagger.v3.oas.annotations.Operation;
public class StudentContraller {
    @Operation(summary = "获取学生信息")
    @GetMapping("/student/{id}/")
    public Response<StudentDTO> getStudentById(@PathVariable long id){}
}

访问

http://localhost:8080/swagger-ui/index.html#/

相关推荐
前端付豪19 分钟前
17、自动化才是正义:用 Python 接管你的日常琐事
后端·python
我是一只代码狗23 分钟前
springboot中使用线程池
java·spring boot·后端
hello早上好36 分钟前
JDK 代理原理
java·spring boot·spring
PanZonghui39 分钟前
Centos项目部署之安装数据库MySQL8
linux·后端·mysql
PanZonghui41 分钟前
Centos项目部署之运行SpringBoot打包后的jar文件
linux·spring boot
PanZonghui41 分钟前
Centos项目部署之Java安装与配置
java·linux
Victor35642 分钟前
MySQL(119)如何加密存储敏感数据?
后端
用户3966144687191 小时前
TypeScript 系统入门到项目实战-慕课网
后端
guojl1 小时前
Dubbo SPI原理与设计精要
后端