Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于分页接口中参数的定义,建议封装成DTO #224

Open
colorful3 opened this issue Jun 27, 2021 · 2 comments
Open

关于分页接口中参数的定义,建议封装成DTO #224

colorful3 opened this issue Jun 27, 2021 · 2 comments

Comments

@colorful3
Copy link
Member

例如如下代码:

@RequestParam(name = "group_id", required = false)
@Min(value = 1, message = "{group.id.positive}") Integer groupId,
@RequestParam(name = "count", required = false, defaultValue = "10")
@Min(value = 1, message = "{page.count.min}")
@Max(value = 30, message = "{page.count.max}") Integer count,
@RequestParam(name = "page", required = false, defaultValue = "0")
@Min(value = 0, message = "{page.number.min}") Integer page) {

我们的项目中的示例代码,但凡碰到分页的相关的传参,都是采用这样的方式定义,将分页的查询参数,定义在了方法参数签名中的,这样虽然比较直观,但无疑增加了代码量。
我们可以封装一个基础分页DTO(例如BasePageDTO),在 DTO 中定义这些属性。如果有额外的参数,可以继承该 DTO。这样既减少了代码量,也提高了代码的封装性。

@gadfly3173
Copy link
Contributor

Get请求参数使用bean接收时会有个问题,此时的参数绑定并不经过jackson,需要增加BindException的异常处理,并且不能自动从snake_case转camelCase,我后来是用一个自定义的ServletModelAttributeMethodProcessor和ServletRequestDataBinder解决的参数绑定。

@colorful3
Copy link
Member Author

Get请求参数使用bean接收时会有个问题,此时的参数绑定并不经过jackson,需要增加BindException的异常处理,并且不能自动从snake_case转camelCase,我后来是用一个自定义的ServletModelAttributeMethodProcessor和ServletRequestDataBinder解决的参数绑定。

欢迎提交pr来贡献你的代码!😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants