java - 在 Swagger 中为请求参数提供示例值

标签 java rest spring-boot swagger

我在 Spring-Boot RestController 中有一个 rest 方法的方法签名,如下所示:

@RequestMapping(
        value = "/path",
        method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_JSON_VALUE,
        produces = MediaType.APPLICATION_JSON_VALUE
)
@ApiImplicitParams({
        @ApiImplicitParam(
                name = "message", 
                value = "Message that is sent to the method", 
                required = true, 
                dataType = "string", 
                paramType = "body"
        )
})
public @ResponseBody String receiveMessage(@RequestBody String message) {
    // ...

    return "{\"success\": true}";
}

我想为 message 参数提供一个“样本”值,它是一个 JSON 字符串(例如 {"key": "value"})。有人知道我如何使用 Swagger 注释来做到这一点吗?我试过了

@ApiImplicitParams({
        @ApiImplicitParam(
                // ...
                example = "...JSON value..."
        )
})

但是没有用。我想要的是文档中的“示例值”,读者可以单击该文档中的参数值字段来填充给定的示例值。这可能吗?

这是它可能看起来的截图:

enter image description here

只是为了防止“无用”的答案:由于我的业务逻辑,我无法将参数类型从 String 更改为某个类类型。

最佳答案

很遗憾,您无法为原子参数 a(字符串、数字...)提供样本或示例值。

如果参数是一个带有模式的对象,你只能提供一个例子,你只需要在属性描述中添加一个example属性:

properties:
  firstName:
    description: first name
    type: string
    example: John

作为最后的手段,您可以在参数的描述中添加示例值(ApiImplicitParam 注释中的 value)。

    @ApiImplicitParam(
            name = "message", 
            value = "Message that is sent to the method. Example: value", 
            required = true, 
            dataType = "string", 
            paramType = "body"
    )

关于java - 在 Swagger 中为请求参数提供示例值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39295531/

相关文章:

Spring Boot ApplicationListener 在应用程序启动时未运行

Java静态分析: Getting started

java - ResolvedModule、Module 和 Observable Module 有什么区别

oop - 是否可以通过 SOA 应用 HATEOAS 原则?

c - 数组删除了在 C 中打印出来的剩余数字

mongodb - 使用 Spring boot 和 mongodb 运行 camunda

java - 这是 Java 中桥接模式的正确实现吗?

java - 理解合并排序的栅栏处理问题

java - 在 Jersey Java 中向 REST WebService 发送和接收 JSON

java - 使用传入的值作为@JmsListener 的目标参数