【发布时间】:2018-04-29 06:39:34
【问题描述】:
我有一个带有 springfox-swagger2 2.7.0 的 Spring Boot 项目,并且我有以下控制器:
@Api(tags = { "Some" }, description = "CRUD for Some Stuff")
@RestController
@RequestMapping(path = "/some")
public class SomeController {
@ApiOperation(value = "Get some")
@GetMapping(value = "{someId}", produces = MediaType.APPLICATION_JSON_VALUE)
public Response getSomeById(@PathVariable("someId") Id someId) {
return ...;
}
...
}
我想通过注释Id 类来控制文档中显示的内容,这仅适用于注释的某些部分,但不是全部。
Id 类(有一个从String 到Id 的注册转换器):
public class Id {
@ApiParam(value = "This is the description", defaultValue = "1f1f1f",required = true, name = "someId", type = "string")
private final Long id;
public Id(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
}
现在返回的Swagger JSON 如下所示:
"parameters":[{
"name":"id",
"in":"query",
"description":"This is the description",
"required":true,
"type":"integer",
"default":"1f1f1f",
"format":"int64"
}]
我的问题(或可能是错误报告)是:为什么使用 @ApiParam 注释的某些部分(如 value、defaultValue 和 required),但其他部分没有使用,如 name 和type?
为什么我似乎无法在此处更改 name 或 type?
对于我的特定用例,后者是我想要更改为 string 的那个。
更新
在 skadya 的帮助下,我决定添加以下组件。
@Component
public class OverrideSwaggerApiParamBuilder implements
ExpandedParameterBuilderPlugin {
@Override
public boolean supports(DocumentationType type) {
return DocumentationType.SWAGGER_2 == type;
}
@Override
public void apply(ParameterExpansionContext context) {
Optional<ApiParam> apiParamOptional = findApiParamAnnotation(context.getField().getRawMember());
if (apiParamOptional.isPresent()) {
ApiParam param = apiParamOptional.get();
context.getParameterBuilder()
.name(param.name())
.modelRef(new ModelRef(param.type()))
.build();
}
}
}
springfox 的作者认为这可能是一个错误:https://github.com/springfox/springfox/issues/2107
【问题讨论】:
标签: java spring-boot swagger swagger-ui swagger-2.0