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#/