gpt4 book ai didi

java - Swagger 示例帖子正文 - 如何显示 JSON Body - Swagger-annotations

转载 作者:行者123 更新时间:2023-12-02 07:31:02 30 4
gpt4 key购买 nike

要求:我有一个 POST 方法,它将输入 JSON 作为字符串并将其传递给另一个微服务。我不想创建此输入 JSON 的对象(Bean)。

方法:

    @ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
@RequestMapping(name = "xxx" value ="/hello" ..)
@ApiResponses(..)
public @ResponseBody String getXXX (@Apiparam(name="JSONrequest", required = true) @RequestBody String JSONrequest){

}

问题:生成的 Swagger 不会将输入显示为显示所有 JSON 属性的 JSON 模型。

期望:我想展示我的 Swagger 类似这样的东西:

enter image description here

我肯定错过了关键的事情。有什么想法吗?

最佳答案

这是一个老问题,但由于我在这里还没有找到在线解决方案,因此我如何自定义由 java 注释自动生成的 swagger 文档中的示例值。我使用 swagger 2.0 和 springfox.version 2.10.5。

想法是记录具有@RequestBody注释的请求参数的类。例如我的方法是

    @ApiOperation(
value = "Start ListBuilder extraction",
response = ExtractionLogEntity.class,
produces = "application/json"
)
@PostMapping("/extraction/start")
public ExtractionLogEntity startTask(
@RequestBody(required = true) ExtractionRequest request,

为了公开请求 json 对象示例,我向 ExtractionRequest 的属性添加了 @ApiModelProperty(example = "...") 注释。

   @ApiModelProperty(example = "[{ 'field':'value'}]")
@NotNull
private List<ListBuilderFieldEntity> fields;

@ApiModelProperty(example = "1000")

private String ied;
@ApiModelProperty(example = "US")
private String codebase;

这就是结果

enter image description here

关于java - Swagger 示例帖子正文 - 如何显示 JSON Body - Swagger-annotations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48039876/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com