您的位置:首页 > 文旅 > 美景 > 西安夜总会预定平台_北京网站设计公司哪儿济南兴田德润简介_头条搜索是百度引擎吗_好的营销网站

西安夜总会预定平台_北京网站设计公司哪儿济南兴田德润简介_头条搜索是百度引擎吗_好的营销网站

2024/10/31 12:52:18 来源:https://blog.csdn.net/weixin_53041251/article/details/143102111  浏览:    关键词:西安夜总会预定平台_北京网站设计公司哪儿济南兴田德润简介_头条搜索是百度引擎吗_好的营销网站
西安夜总会预定平台_北京网站设计公司哪儿济南兴田德润简介_头条搜索是百度引擎吗_好的营销网站

1、环境

Java: 21
Spring Boot: 3.3.4
Maven: 3.9.9

2、实操

2.1 引入依赖

<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-openapi3-jakarta-spring-boot-starter -->
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId><version>4.5.0</version>
</dependency>

2.2 添加配置文件

package com.jie.springbootinit.config;import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Map;/*** Knife4j配置* * @author jie*/
@Configuration
public class SwaggerConfig {/*** API标题*/private static final String API_TITLE = "Jie的API";/*** API版本*/private static final String API_VERSION = "1.0";/*** API描述*/private static final String API_DESCRIPTION = "Knife4j集成springdoc-openapi示例";/*** 服务条款URL*/private static final String TERMS_OF_SERVICE_URL = "http://doc.xiaominfo.com";/*** 许可证名称*/private static final String LICENSE_NAME = "Apache 2.0";/*** 许可证URL*/private static final String LICENSE_URL = "http://doc.xiaominfo.com";/*** 根据@Tag 上的排序,写入x-order** @return the global open api customizer*/@Beanpublic GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {return openApi -> {if (openApi.getTags() != null) {openApi.getTags().forEach(tag -> {// 使用Map.of简化Map的创建tag.setExtensions(Map.of("x-order", 1));});}if (openApi.getPaths() != null) {openApi.addExtension("x-test123", "333");openApi.getPaths().addExtension("x-abb", 1);}};}@Beanpublic OpenAPI createCustomOpenApi() { // 修改方法名为createCustomOpenApireturn new OpenAPI().info(new Info().title(API_TITLE).version(API_VERSION).description(API_DESCRIPTION).termsOfService(TERMS_OF_SERVICE_URL).license(new License().name(LICENSE_NAME).url(LICENSE_URL)));}
}

2.3 添加测试的Controller

package com.jie.springbootinit.controller;import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 测试接口* * @author jie*/
@RestController
@RequestMapping("/test")
@Tag(name = "测试接口")
public class TestContrller {@GetMapping("/hello")@Operation(summary = "测试接口")public String hello() {return "Hello, World!";}
}

3、启动项目,查看效果

访问地址:http://localhost:项目端口/doc.html#/home

发送请求

4、Swagger V3(OpenAPI 3)常用注解

注解说明示例使用位置
@OpenAPIDefinition用于定义整个API的元数据,如信息、服务器等@OpenAPIDefinition(info = @Info(title = "API Title", version = "1.0"))类级别(通常是主应用类)
@Info定义API的基础信息,如标题、版本、描述等@Info(title = "API Title", version = "1.0", description = "API Description")作为@OpenAPIDefinition的属性
@Server指定API服务器的基本信息,包括URL和描述@Server(url = "http://localhost:8080", description = "Local server")类级别或方法级别
@Tag用于为API分组和标记@Tag(name = "User Operations", description = "Operations related to users")类级别或方法级别
@Operation用于描述API操作(即每个HTTP请求)@Operation(summary = "Get user by ID", description = "Returns a user by their ID")方法级别
@Parameter描述操作中的参数@Parameter(name = "id", description = "User ID", required = true)方法级别的参数
@RequestBody描述API的请求体@RequestBody(description = "User object that needs to be added", required = true)方法级别
@ApiResponse用于定义每个操作的响应信息@ApiResponse(responseCode = "200", description = "Successful operation")方法级别
@Schema定义模型对象的属性和元数据@Schema(description = "User model", required = true)类级别、字段级别
@ArraySchema用于定义数组类型的Schema@ArraySchema(schema = @Schema(implementation = User.class))方法级别、字段级别
@Content用于指定响应和请求体的媒体类型及内容@Content(mediaType = "application/json", schema = @Schema(implementation = User.class))作为@RequestBody@ApiResponse的属性
@ExampleObject用于定义请求或响应的示例@ExampleObject(name = "Example 1", value = "{\"id\": 1, \"name\": \"John\"}")作为@Content的属性
@Components定义API中的可重用组件,如schemas、参数、响应等@Components(schemas = @Schema(name = "User", ...))类级别(通常是主应用类)
@SecurityRequirement指定操作所需的安全方案@SecurityRequirement(name = "bearerAuth")方法级别或类级别
@Header定义操作中使用的响应头信息@Header(name = "X-Rate-Limit", description = "Request rate limit")作为@ApiResponse的属性

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com