【问题标题】:How to Swagger Annotate a Spring GET @RequestMapping having a complex Object如何 Swagger 注释具有复杂对象的 Spring GET @RequestMapping
【发布时间】:2017-06-30 15:25:17
【问题描述】:

问题是我有一个复杂的对象作为 GET 请求的请求参数,并且在我将 Swagger 注释放在对象中之后。 Swagger UI 显示入口参数是我必须在其中放置参数的主体。

 body: {
      "modelId": 0,
      "makeId": 0
    }

我的 REST 控制器如下所示

  @RequestMapping(method = RequestMethod.GET, value = "/model")
  public SearchModelsResponse searchModels(
      @ApiParam(value = "Search for something",
          required = true) final ModelSearch search) {...}

还有请求对象

public class ModelSearch {

  @ApiParam(value = "Something important)", required = true)
  private Long modelId;

  @ApiParam(value = "Something else important)", required = false)
  @Nullable
  private Long makeId;

  ....
  }

有没有办法正确注释它,以便 Swagger 将其显示为请求参数而不是正文构造?

【问题讨论】:

  • 对象作为请求参数?我不这么认为。仅支持原语作为请求参数。
  • 它在 Spring 方面运行良好,Jackson 在反序列化过程中没有问题。唯一打破的就是大摇大摆。
  • 我的意思是 swagger 不支持它。在春天,你可以拥有你想要的任何东西。
  • 这种实现方式在 GET 方法中很常见吗?我还看到了一些使用反序列化对象而不是原语的实现。

标签: java spring spring-mvc swagger swagger-ui


【解决方案1】:

好的,这种情况下的解决方案是手动定义参数,这可以通过@ApiImplicitParam注解来实现。

所以结果看起来像这样。

  @ApiImplicitParams({
    @ApiImplicitParam(name = "modelId", value = "this is modelId", required = true, dataType = "string", paramType = "query"),
    @ApiImplicitParam(name = "makeId", value = "this is makeId", required = true, dataType = "string", paramType = "query")
  })
  @RequestMapping(method = RequestMethod.GET, value = "/model")
  public SearchModelsResponse searchModels(
      final ModelSearch search) {...}

这不是一个漂亮的解决方案,因为我实际上希望 swagger 解释我的代码,但结果提供了将其显示为请求参数而不是正文构造的选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 2018-11-27
    • 2018-04-18
    • 2013-06-01
    • 1970-01-01
    • 2020-02-18
    相关资源
    最近更新 更多