Swagger学习⑧——@Parameters注解

介绍

@Parameters 注解是 Swagger Core 库的一部分,属于 Swagger/OpenAPI 3.x。它用于在 API 操作中同时描述多个参数。与 @Parameter 注解不同,@Parameters 是一个容器注解,可以包含多个 @Parameter 注解,从而更简洁地描述多个参数。

作用

@Parameters 注解的主要作用是将多个 @Parameter 注解组合在一起,方便在一个方法中描述多个参数。它通常用于以下场景:

  • 当一个 API 操作需要多个参数时,可以使用 @Parameters 来集中管理这些参数的元数据。

  • 提高代码的可读性和可维护性。

源代码

package io.swagger.v3.oas.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Parameters {
    Parameter[] value() default {};
}

示例代码

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@Tag(name = "Demo管理", description = "Demo相关的 API")
public class DemoController {

    @Operation(
        summary = "搜索Demo",
        description = "根据多个条件搜索Demo",
        responses = {
            @ApiResponse(responseCode = "200", description = "成功搜索到Demo")
        }
    )
    @Parameters({
        @Parameter(
            name = "name",
            description = "Demo名",
            required = false,
            example = "John",
            in = ParameterIn.QUERY
        ),
        @Parameter(
            name = "info",
            description = "Demo info",
            required = false,
            example = "25",
            in = ParameterIn.QUERY
        ),
        @Parameter(
            name = "city",
            description = "Demo所在城市",
            required = false,
            example = "New York",
            in = ParameterIn.QUERY
        )
    })
    @GetMapping("/demos/search")
    public String searchDemors(
        @RequestParam(required = false) String name,
        @RequestParam(required = false) Integer info,
        @RequestParam(required = false) String city) {
        return "Searching for Demo with name: " + name + ", info: " + info + ", city: " + city;
    }
}




### 关于 `@Parameter` 注解Java 的 Spring 生态系统中,`@Parameter` 并不是一个标准的 Spring 提供的核心注解。然而,在某些特定场景下,它可能被用于参数绑定或其他框架扩展功能中。例如,在 MyBatis 框架中,`@Param` 是一个常用的注解,而有时开发者可能会误将其称为 `@Parameter`。 以下是关于 `@Param` 或类似的自定义实现方式的一个示例: #### 参数绑定示例 假设我们正在使用 MyBatis 进行数据库操作,可以通过如下代码片段来理解其作用: ```java import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; public interface UserMapper { @Select("SELECT * FROM users WHERE username = #{username} AND password = #{password}") User findUser(@Param("username") String userName, @Param("password") String passWord); } ``` 在这个例子中,`@Param` 被用来指定 SQL 查询语句中的占位符名称与方法参数之间的映射关系[^1]。 如果确实需要创建自己的 `@Parameter` 注解,则可以按照以下方式进行定义和使用: #### 自定义 `@Parameter` 注解 ##### 定义注解 ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface Parameter { String value(); } ``` 上述代码定义了一个简单的 `@Parameter` 注解,其中包含一个名为 `value` 的属性。 ##### 使用自定义注解 接下来可以在控制器或者服务层的方法中应用该注解: ```java @RestController @RequestMapping("/api") public class CustomController { @PostMapping("/example") public ResponseEntity<String> handleRequest( @Parameter("name") @RequestParam(required = false) String name, @Parameter("age") @RequestParam int age) { return ResponseEntity.ok(String.format("Name: %s, Age: %d", name, age)); } } ``` 在此处,通过组合使用 `@Parameter` 和其他 Spring MVC 注解(如 `@RequestParam`),能够更清晰地标记每个参数的意义及其用途[^2]。 #### 异步处理补充说明 虽然本话题主要围绕 `@Parameter` 展开讨论,但值得一提的是,在涉及异步任务时,Spring 提供了专门的 `@Async` 注解支持。这允许我们将某个方法标记为异步执行,从而提高程序性能和响应速度[^3]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值