如何 Swagger 注释具有复杂对象的 Spring GET @RequestMapping
How to Swagger Annotate a Spring GET @RequestMapping having a complex Object
问题是我有一个复杂的对象作为 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 将其显示为请求参数而不是正文结构?
好的,这种情况下的解决方案是手动定义参数,这可以通过 @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) {...}
这不是一个完美的解决方案,因为我实际上想要大摇大摆地解释我的代码,但结果提供了将其显示为请求参数而不是主体构造的选项。
问题是我有一个复杂的对象作为 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 将其显示为请求参数而不是正文结构?
好的,这种情况下的解决方案是手动定义参数,这可以通过 @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) {...}
这不是一个完美的解决方案,因为我实际上想要大摇大摆地解释我的代码,但结果提供了将其显示为请求参数而不是主体构造的选项。