Swagger 3和SpringMVC如何描述Pojo RequestParams

发布于 2025-01-22 09:08:56 字数 630 浏览 0 评论 0原文

我有一个简单的SpringMVC控制器

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

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

,因为该端点具有约20个请求Param,而不是用所有的字段将它们都放在POJO中的所有字段(实际上可以在需要类似的查询参数过滤器的其他控制器中重复使用),而不是将控制器膨胀。

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

现在的问题是,Swagger不认为userFilter及其字段是查询参数,而是一个简单的对象,因此在Swagger UI上,它变得毫无用处,因为很难测试该端点。

有没有办法指示Swagger UserFilter字段需要视为查询参数?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然 2025-01-29 09:08:56

魔术注释是@parameterObject

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

Magic annotation is @ParameterObject

@GetMapping
  public String getAllUsers(@Valid @ParameterObject Filter filter) throws MyCustomServiceException {
   [...]
  }
ˉ厌 2025-01-29 09:08:56

您可以在用@modelattribute注释的模型类中使用@apiparam注释,因此Swagger将此字段生成UI。

public class UserFilter {
    @ApiParam(value = "the user email", required = true)
    private String email;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文