【问题标题】:Display API Request/Response structure using swagger annotations for Java使用 Java 的 swagger 注释显示 API 请求/响应结构
【发布时间】:2018-05-30 22:47:01
【问题描述】:

我是 swagger 的新手,我正在尝试向 API 文档添加一个使用 swagger.io Annotations 库的 API 的示例输入/输出 json。我尝试过这样的事情:

@other annotations here
public ResponseObj doSomething(@ApiParam(name = "testname", value = 
"test value", required = true, example = "{\"userId\":\"1234\"}" 
@RequestBody RequestObj req) {
    //some code here
}

在输出文档中,我只看到名称、值和必填字段,而不是示例。我什至尝试过

examples = @Example(value = { @ExampleProperty(value = "examplepropvaluetest") } 

但也没有看到。

帮助任何人? TIA

【问题讨论】:

  • 你用的是spring-fox。使用 spring,spring-fox swager 集成效果更好
  • 是的,我正在使用 spring-fox
  • 如果你使用的是springfox,那么添加@RequestBody就足够了,它会显示在ui中
  • 我正在使用@RequestBody,如我的问题所示。它只是显示模式(如在表格中),而不是 json 格式。

标签: java annotations swagger


【解决方案1】:

Swagger 将任何请求/响应显示为模型(表格)和示例值 (json)。 请参阅下面的屏幕截图。

作为模特

作为示例值(xml / json)

单击模型/示例值在它们之间切换。

【讨论】:

    【解决方案2】:

    我通过使用 spring-restdocs(https://github.com/spring-projects/spring-restdocs) 解决了这个问题。基本上,您在运行测试时生成文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      相关资源
      最近更新 更多