问题
How can I make Swagger document a String resource parameter as a full class type?
I.e. I have this resource declaration:
@PatchMapping(path="/{id}")
public ApiResponse<MyObject> patch(@PathVariable String id, @RequestBody String myObjText) {
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
seems to be ignored for @RequestBody
parameters, and none of @ApiModel*
annotations are allowed there.
I'm using springfox
since these are Spring Rest resources.
回答1:
Try to override String param as @ApiParam(hidden = true)
and add
new param for your object:
@ApiImplicitParams({
@ApiImplicitParam(name = "My obj text",
value = "myObjText", required = true,
dataType = "com.example.MyObject", paramType = "body")
})
Like it is implemented here: How to document implicitly dto usage, when we use entity class as api param?
回答2:
In my case two changes needed to be made:
1) Added @ImplicitParams
(type name is not qualified with package name)
@ApiImplicitParams({
@ApiImplicitParam(name = "requestBody", required = true,
dataType = "MyObject", paramType = "body")
})
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));
}
}
来源:https://stackoverflow.com/questions/51098932/how-to-make-springfox-swagger-render-a-string-parameter-as-another-model-type