java 如何在 Spring Controller 中检索 FORM/POST 参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31594608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:50:28  来源:igfitidea点击:

How to retrieve FORM/POST Parameters in Spring Controller?

javaspringspring-mvcmailchimpform-parameter

提问by WowBow

I have the following response that should come from a MailChimpwebhook URL.

我有以下响应应该来自MailChimpwebhook URL。

This is the row BODY:

这是行 BODY:

RAW BODY

    type=usub&fired_at=2015-07-23+17%3A19%3A34&data%5Baction%5D=unsub&data%5Breason%5D=manual&data%5Bid%5D=9383uy636&data%5Bemail%5D=youremail%40YOURDOMAIN.com&data%5Bemail_type%5D=html&data%5Bip_opt%5D=202.9.3.003&data%5Bweb_id%5D=404004&data%5Bmerges%5D%5BEMAIL%5D=YOUREMAIL%40YOURDOMAIN.com&data%5Bmerges%5D%5BFNAME%5D=NAME&data%5Bmerges%5D%5BLNAME%5D=LASTNAME&data%5Blist_id%5D=2288883
 FORM/POST PARAMETERS

fired_at: 2015-07-22 12:19:34
data[email]: [email protected]
data[id]: 56775409ta
data[web_id]: 09833944
data[merges][EMAIL]: [email protected]
type: unsub
data[list_id]: 99884hy372
data[merges][FNAME]: Name
data[ip_opt]: 202.0.9.3333
data[reason]: manual
data[email_type]: html
data[action]: unsub
data[merges][LNAME]: LastName

**QUERYSTRING key: a4483983hu473004884j0x**

HEADERS

Accept: */*
Total-Route-Time: 0
Host: requestb.in
Connection: close
Content-Length: 395
User-Agent: MailChimp.com
Connect-Time: 0
Via: 1.1 vegur
X-Request-Id: 6633d8-653e-4cea-884j-9933ju4773h
Content-Type: application/x-www-form-urlencoded

I have used @RequestParameterpreviously when working with Spring Controllers, however I am not sure how to get the data from the above response for e.g data[merges][FNAME]:

@RequestParameter以前在使用 Spring 控制器时使用过,但是我不确定如何从上述响应中获取数据,例如data[merges][FNAME]

How can I also get the QueryString inside a Spring controller ? QUERYSTRING key: a4483983hu473004884j0x

我怎样才能在 Spring 控制器中获取 QueryString ?查询字符串键:a4483983hu473004884j0x

//I have the following code to begin with
@RequestMapping("/unsubscribewebhook")
    public ZapJasonMessage unsubscribeWebHook(
    @RequestParam("key") String key,
    @RequestParam ("data[merges][FNAME]") String firstName
) {

}

I appreciate your help!

我感谢您的帮助!

采纳答案by óscar Ibá?ez Fernández

You just need to inject WebRequest as a parameter into your controller POST method, and then ask for getParameter(), with a given parameter from the Mailchimp webhook.

您只需要将 WebRequest 作为参数注入控制器 POST 方法,然后使用 Mailchimp webhook 中的给定参数请求 getParameter()。

e.g:

例如:

@RequestMapping(path="/MyUrl", method=RequestMethod.POST)
public ModelAndView process(WebRequest request){
    System.out.println(request.getParameter("type"));
    System.out.println(request.getParameter("data[merges][EMAIL]"));
    return new ModelAndView([YOURVIEWHERE]);
}

That's all...

就这样...

Regards.

问候。