Swagger 3和SpringMVC如何描述Pojo RequestParams
我有一个简单的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
魔术注释是
@parameterObject
Magic annotation is
@ParameterObject
您可以在用
@modelattribute
注释的模型类中使用@apiparam
注释,因此Swagger将此字段生成UI。You can use
@ApiParam
annotation in your model class annotated with@ModelAttribute
so swagger generates this field into the UI.