How to make SpringFox Swagger render a string parameter as another model type?

时光总嘲笑我的痴心妄想 提交于 2021-01-27 08:15:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!