1

I have a simple SpringMVC controller

@RestController
@Validated
@RequestMapping("/users")
public class UsersController {

  @GetMapping
  public String getAllUsers(@Valid Filter filter) throws MyCustomServiceException {
   [...]
  }
}

Since this endpoint has around 20 RequestParam instead of bloating the controller(s) with all the fields I have put them all nicely in a POJO (that actually can be reused in other controllers that needs similar query params filters)

public class UserFilter extends GenericRequestParams {
  [...] 
  private String email;
  [...] 
}

Now the problem is that Swagger doesn't consider that UserFilter and its fields to be query params but a simple Object so on the Swagger UI it become useless since it's hard to test that endpoint.

Is there a way to instruct swagger that UserFilter fields needs to be considered query params?

Alexis
  • 1,825
  • 4
  • 23
  • 28

2 Answers2

3

Magic annotation is @ParameterObject

@GetMapping
  public String getAllUsers(@Valid @ParameterObject Filter filter) throws MyCustomServiceException {
   [...]
  }
Alexis
  • 1,825
  • 4
  • 23
  • 28
0

You can use @ApiParam annotation in your model class annotated with @ModelAttribute so swagger generates this field into the UI.

public class UserFilter {
    @ApiParam(value = "the user email", required = true)
    private String email;
}
Juan Rada
  • 3,513
  • 1
  • 26
  • 26
  • 1
    Swagger 3 doesn't have ApiParam (its equivalent is `@Parameter) but unfortunately I have tried using "@ModelAttribute" in the controller but Swagger still didn't consider these as query params – Alexis Apr 19 '22 at 09:27