首页 文章

如何用Swagger描述“ModelAttribute”的最佳方法

提问于
浏览
4

我正在尝试将Swagger2集成到基于Spring Boot的应用程序中 . 问题是招摇不考虑模型属性 .

@GetMapping(value = "/events", produces = MediaType.APPLICATION_JSON_VALUE)
public PagedResources<EventResource> getEvents(
        @Valid SearchCriteria searchQuery, 
        BindingResult result, 
        PageableResourcesAssembler<EventResource> assembler){

    // code
}

正如您所看到的, SearchCriteria 是一个由Spring自动绑定的类 .

public class SearchCriteria {

    private List<EventType> eventTypes;

    private LocalDateTime from;

    // getters setters
}

但是招摇所产生的是:

enter image description here

这不是预期的 . 期望的结果可能由注释 getEvents 方法生成

@ApiImplicitParams({
    @ApiImplicitParam(name = "eventTypes", paramType = "query"),
    @ApiImplicitParam(name = "from", paramType = "query")
})
PagedResources<EventResource> getEvents(@ApiParam(hidden = true ) @Valid SearchCriteria searchQuery

但是 @ApiParam(hidden = true ) 不起作用,因为在Swagger UI中仍然存在 searchQuery 参数 .


使用swagger描述POJO中包含的请求参数的正确方法是什么?对我来说,最好的方法是注释 SearchCriteria 类与 @ApiModel ,但它不起作用 .

1 回答

  • 2

    This bug was fixed in Springfox v2.7.0.


    原答案:

    @Valid -annotation实际上确实将param视为body-param .

    因为这不应该这样做I've opened an issue on the springfox github page .

    但@ApiParam(hidden = true)不起作用

    Springfox提供了应该起作用的 springfox.documentation.annotations.ApiIgnore -annotation .

    喜欢用this issue编写,使用springfox的注释是正确的方法 .

相关问题