如何将所有用@RequestParam 注释的字段收集到一个对象中 [英] How to collect all fields annotated with @RequestParam into one object

查看:107
本文介绍了如何将所有用@RequestParam 注释的字段收集到一个对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将所有查询参数收集到一个 pojo 中,并对字段执行额外的验证.

I would like to gather all of my query parameters into a pojo and perform additional validation of the fields.

我已经读到我可以简单地创建一个对象,spring-boot 会自动在其上设置这些请求参数.

I have read that I can simply create an object and spring-boot will automatically set those request parameters on it.

@GetMaping公共响应实体listEntities(@RequestParam(value = "page-number", defaultValue = "0") @Min(0) Integer pageNumber,@RequestParam(value = "page-size", defaultValue = "100") @Min(1) Integer pageSize ... )

我正在考虑创建一个名为 RequestParamsDTO 的类,其中我的查询参数负责分页.

I am thinking to create a class called RequestParamsDTO, where I'd have my query params responsible for the pagination.

但是为了在 RequestParamsDTO 上设置这些字段,我必须将请求参数的名称与字段名称匹配.但它不会是一个有效的变量名:page-size.

But in order to have those fields set on the RequestParamsDTO, I'd have to match the name of the request param with the field name. But it won't be a valid variable name: page-size.

必须有一些解决方法,类似于 @RequestParam 的 value 属性,可以在 DTO 中的我的字段上设置给定的请求参数.

There must be some workaround, similar to @RequestParam's value attribute, that would set given request param on my field in the DTO.

请指教.

推荐答案

有人已经打算这样做 功能 之前,您可以执行以下操作.但不幸的是,由于不活动响应而被拒绝:

Someone already purposed this feature before such that you can do the following .But unfortunately it is declined due to inactivity response :

public class RequestParamsDTO{
   @RequestParam(value="page-number",defaultValue="0")
   @Min(0)
   private Integer pageNumber;

   @RequestParam(value = "page-size", defaultValue = "100") 
   @Min(1) 
   Integer pageSize 
}

您可以做的最相似的事情是使用它的 @ModelAttribute,它将解析以下 订单:

The most similar things that you can do is using its @ModelAttribute which will resolve the parameter in the following orders:

  • 如果已使用模型添加,则来自模型.
  • 使用@SessionAttributes 来自 HTTP 会话.
  • 来自通过转换器传递的 URI 路径变量(参见下一个示例).
  • 来自调用默认构造函数.
  • 来自调用具有与 Servlet 请求参数匹配的参数的主构造函数".参数名称通过 JavaBeans @ConstructorProperties 或通过字节码中运行时保留的参数名称确定.

这意味着 RequestParamsDTO 不能没有任何默认构造函数(没有参数的构造函数).它应该有一个主构造函数",你可以使用 @ConstructorProperties定义哪些请求参数映射到构造函数参数:

That means the RequestParamsDTO cannot not have any default constructor (constructor that is without arguments) .It should have a "primary constructor" which you can use @ConstructorProperties to define which request parameters are mapped to the constructor arguments :

public class RequestParamsDTO{
    @Min(0)
    Integer pageNumber;
    @Min(1)
    Integer pageSize;

    @ConstructorProperties({"page-number","page-size"})
    public RequestParamsDTO(Integer pageNumber, Integer pageSize) {
        this.pageNumber = pageNumber != null ? pageNumber : 0;
        this.pageSize = pageSize != null ? pageSize : 100;
    }
}

控制器方法变为:

@GetMaping
public ResponseEntity<?> listEntities(@Valid RequestParamsDTO request){

}

注意事项:

  • @RequestParamdefaultValue没有等价的注解,需要在构造函数中手动实现.

  • There is no equivalent annotation for @RequestParam 's defaultValue,so need to implement in the constructor manually.

如果控制器方法参数与 this ,它会解析为 @ModelAttribute ,即使 @ModelAttribute 没有明确注释.

If the controller method argument does not match the values in this , it will resolved as @ModelAttribute even though @ModelAttribute is not annotated on it explicitly.

这篇关于如何将所有用@RequestParam 注释的字段收集到一个对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆