0

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.

Helen
  • 71,797
  • 10
  • 199
  • 256
A_G
  • 1,964
  • 1
  • 17
  • 39

2 Answers2

2

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) {
Gaurav Dhiman
  • 823
  • 5
  • 10
  • But @Gaurav this actually is a use case for making a request, so ideally i shouldn't have to convert it to post. Is there no way we can achieve the same with ModelAttribute? – A_G Oct 21 '20 at 17:46
  • whenever we make a request and when the request have body we should go with PostMapping and in case of REST APIs best practice is to use RequestBody to map the request object – Gaurav Dhiman Oct 21 '20 at 17:50
  • yeah so that's why I was hoping to `ModelAttribute` instead of RequestBody to keep it `GET` request conventionally. – A_G Oct 21 '20 at 17:59
  • with GET it won't work for swagger, you will have to change it to POST. – Gaurav Dhiman Oct 21 '20 at 18:36
  • actually it does, I posted the solution above. Immutables are not supported :( – A_G Oct 21 '20 at 18:55
0

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;
}
A_G
  • 1,964
  • 1
  • 17
  • 39