自学内容网 自学内容网

Spring Boot 3项目使用Swagger3教程

Spring Boot 3项目使用Swagger3教程

Swagger:自动生成接口文档

添加依赖(pom.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的配置信息。

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"));
    }
}

接口注释

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#/
在这里插入图片描述


原文地址:https://blog.csdn.net/qq_59636442/article/details/142314184

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!