Swagger中不可见ModelAttribute

问题描述

我正在使用Spring 2.3,并使用

    implementation('io.springfox:springfox-swagger2:2.8.0')
    implementation('io.springfox:springfox-swagger-ui:2.8.0')

我想从UI接受分页参数,模型是

@Immutable
@JsonSerialize(as = ImmPaginationParameters.class)
@JsonDeserialize(as = ImmPaginationParameters.class)
public interface PaginationParameters {
    Integer getLimit();
    Integer getoffset();
}

在控制器中,我将该方法声明为

    @GetMapping("/preview")
    @NonNull
    ResponseEntity<List<SomeDto>> getPreviewResults(@PathVariable final String projectId,@modelattribute final PaginationParameters params) {

但是,在Swagger用户界面中并没有体现出同样的含义。

enter image description here

enter image description here

我尝试将其转换为帖子映射,并在@Valid前面也添加PaginationParameters,但还是一样。检查了api文档,再次没有分页参数的迹象。我检查了帖子What is @ModelAttribute in Spring MVC?,这种定义modelattribute的方式看起来就足够了。不确定我是否必须在PaginationParams类中声明其他内容

解决方法

尝试将@RequestBody与@ApiParam一起使用,而不是@ModelAttribute,并在传递请求正文时也使用@Postmapping:-

@PostMapping("/preview")
@NonNull
ResponseEntity<List<SomeDto>> getPreviewResults(@ApiParam @PathVariable final String projectId,@ApiParam @RequestBody final PaginationParameters params) {
,

结果证明ModelAttribute与Immutable类不能很好地配合使用。将其更改为lombok后,它可以正常工作

@Data
public class PaginationParameters {
    private Integer limit;
    private Integer offset;
}