ModelAttribute not visible in Swagger

1.2k Views Asked by At

I am using Spring 2.3 and imported swagged using

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

I want to accept pagination params from the UI, and the model is

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

In the controller I have declared the method as

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

However, the same does not get reflected in the Swagger UI.

enter image description here enter image description here

I tried converting it into a post mapping and add @Valid too in front of PaginationParameters but the same thing. Checked the API docs, again no sign of pagination params. I checked the post What is @ModelAttribute in Spring MVC? and this way of defining modelAttribute looks sufficient. Not sure I have to declare something different in the PaginationParams class.

2

There are 2 best solutions below

1
On

Turns out that ModelAttribute doesn't work well with Immutable class. After changing it to lombok, it worked

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

Try using @RequestBody along with @ApiParam instead of @ModelAttribute, also use @Postmapping as you are passing request body :-

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