Spring @RequestParam Map< String,String>在POST方法中不起作用 [英] Spring @RequestParam Map<String, String> does not work in POST method

查看:1914
本文介绍了Spring @RequestParam Map< String,String>在POST方法中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Spring文档中我采取了以下内容:

from the Spring documentation I took the following:


public @interface RequestParam

注释,表示方法参数应绑定到Web请求参数。支持Servlet和Portlet环境中带注释的处理程序方法。

Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in Servlet and Portlet environments.

...

如果方法参数是Map或者未指定MultiValueMap和参数名称,然后使用所有请求参数名称和值填充map参数。

If the method parameter is Map or MultiValueMap and a parameter name is not specified, then the map parameter is populated with all request parameter names and values.

现在我有为测试目的创建了一个控制器。它有一个GET和一个POST方法,每个都使用 @RequestParam java.util.Map 作为唯一参数。在方法体中,我只是尝试打印地图的大小。当我仅在GET方法中发送请求(GET / POST)时,映射包含任何键/值对。我正在使用Firefox中的Poster插件,我正在发送三个参数。

Now I have created a controller for test purposes. It has a GET and a POST method and each uses a @RequestParam java.util.Map as only parameter. In the method body I am only trying to print the size of the map. When I send requests (GET/POST) only in the GET method the map contains any key/value pairs. I am using the Poster add-on in Firefox and I am sending three parameters.

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

    @RequestMapping(value="test/method", method = RequestMethod.GET)
    @ResponseBody
    public String testmethodGet(@RequestParam Map<String, String> params) {

        System.out.println("GET: " + params.size()); // prints GET: 3   
        return "";

    }

    @RequestMapping(value="test/method", method = RequestMethod.POST)
    @ResponseBody
    public String testmethodPost(@RequestParam Map<String, String> params) {

        System.out.println("POST: " + params.size()); // prints POST: 0
        return "";

    }

}

任何一个你们知道为什么@RequestParam Map不能用于POST请求,或者我是否需要更改某些内容以使其正常工作?

Would any of you guys know why @RequestParam Map would not work with a POST request or whether I need to change something to make it work?

谢谢。

推荐答案

实际上,它适用于GET和POST方法。这完全是我的错。当您实际将参数传递给POST请求时,最初给定的代码将起作用。

Actually, it works on GET and POST method. It was solely my fault. The initially given code will work, when you actually pass parameters to the POST request.

考虑以下JS(jQuery)代码作为如何发送有效请求:

Consider the following JS ( jQuery ) code as how to send a valid request:

$.ajax({
    type: "POST",
    url: "test/method",
    data: { param1: param1, param2: param2, param3: param3 },
    success: function(data) {
        console.log("testPost successful!");
    },    
    dataType: "html", // expected return value type
    error: function(data, status, error) {
        console.log("testPost with errors!");
    }
});

这篇关于Spring @RequestParam Map&lt; String,String&gt;在POST方法中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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