如何让 SpringFox Swagger 将字符串参数渲染为另一种模型类型? [英] How to make SpringFox Swagger render a string parameter as another model type?
问题描述
如何使 Swagger 将 String 资源参数记录为完整的类类型?
How can I make Swagger document a String resource parameter as a full class type?
即我有这个资源声明:
@PatchMapping(path="/{id}")
public ApiResponse<MyObject> patch(@PathVariable String id, @RequestBody String myObjText) {
我希望 myObjText
被记录为具有 MyObject
类型的模型,同时仍然能够在方法主体中获取原始 JSON 文本(那是因为我后来想要在 Jackson objectMapper
上调用 readerForUpdating()
.
I want myObjText
to be documented as having a model of type MyObject
while still be able to get the raw JSON text in the method body (that's because I later want to call readerForUpdating()
over a Jackson objectMapper
).
@ApiParam
似乎被 @RequestBody
参数忽略了,并且那里不允许使用任何 @ApiModel*
注释.
@ApiParam
seems to be ignored for @RequestBody
parameters, and none of @ApiModel*
annotations are allowed there.
我正在使用 springfox
因为这些是 Spring Rest 资源.
I'm using springfox
since these are Spring Rest resources.
推荐答案
就我而言,需要进行两个更改:
In my case two changes needed to be made:
1) 添加了@ImplicitParams
(类型名称没有用包名限定)
1) Added @ImplicitParams
(type name is not qualified with package name)
@ApiImplicitParams({
@ApiImplicitParam(name = "requestBody", required = true,
dataType = "MyObject", paramType = "body")
})
2) 注册隐式模型类型(特别是当响应类型是 ResponseEntity(Void.class)
2) Registered implicit model type (especially when response type is ResponseEntity(Void.class)
@Configuration
public class SwaggerConfiguration {
@Autowired
private TypeResolver typeResolver;
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.additionalModels(typeResolver.resolve(MyObject.class));
}
}
这篇关于如何让 SpringFox Swagger 将字符串参数渲染为另一种模型类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!