You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringDoc响应中如何配置多个@ExampleObject?

SpringDoc 配置多个响应示例的正确方式

要让多个@ExampleObject在Swagger文档中生效,核心是给每个示例分配唯一的name属性——SpringDoc依赖这个属性区分不同示例,不指定的话会出现示例覆盖或不显示的问题。

修改后的代码示例:

@ApiResponses( value = {
    @ApiResponse(responseCode = "200", description = "OK"),
    @ApiResponse(responseCode = "400",
            description = "BAD REQUEST",
            content= @Content(
                    mediaType = "application/json",
                    examples = {
                            @ExampleObject(
                                    name = "权限与角色类型不匹配",
                                    value="{\"code\":400,\"Message\":\"Permission and Role type doesn't match\"}"
                            ),
                            @ExampleObject(
                                    name = "权限ID必须是UUID格式",
                                    value="{\"code\":400,\"Message\":\"Permission must be UUID value\"}"
                            )
                    }
            )),
    @ApiResponse(responseCode = "404",
            description = "Permission not found",
            content= @Content(
                    mediaType = "application/json",
                    examples = {
                            @ExampleObject(
                                    value="{\"code\":404,\"Message\":\"Permission not found\"}"
                            )
                    }
            ))
})

关键说明:

  • 每个@ExampleObject的name值需唯一,Swagger UI会以标签形式展示这些名称,方便用户切换查看不同示例
  • 单个示例场景下可以省略name,但多示例场景必须指定

内容的提问来源于stack exchange,提问作者Albert Lam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 15:57:06