Spring MVC bean映射到类似于@BeanParam的HTTP GET请求参数 [英] Spring MVC bean mapping to HTTP GET request parameters similar to @BeanParam

查看:360
本文介绍了Spring MVC bean映射到类似于@BeanParam的HTTP GET请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在泽西岛,有一个@BeanParam批注,通过它我可以将请求参数映射到bean属性.

In Jersey there is @BeanParam annotation with which I can have request parameters mapped to bean attributes.

在春季,我只能找到@RequestBody,它显然适用于请求正文而不适用于请求参数.

In Spring I can find only @RequestBody which obviously works with request body and not request parameters.

是否可以使用Spring将请求参数映射到Bean?

Is there a way to have request parameters mapped to a bean using Spring?

推荐答案

只需使用名称与您的请求参数相匹配的字段来创建Pojo Java Bean.

Simply create a Pojo Java Bean with fields with names that match your request parameters.

然后将此类用作请求处理程序方法的参数(不带任何其他批注)

Then use this class as an argument for your request handler method (without any additional annotations)

public class Example {
   private String x;
   private Integer y;

   //Constructor without parameter needed!
   public Example(){}

   //Getter + Setter
}

@Controller
@RequestMapping("someUrl")
public class SomeController {

    @RequestMapping
    public String someHandler (Example example) {
          System.out.println(example.getX());
          return "redirect:someOtherUrl";
    }
}

这篇关于Spring MVC bean映射到类似于@BeanParam的HTTP GET请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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