java Spring Boot - 如何在 Spring RestController 的地图中获取所有请求参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42823730/
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-03 06:53:14  来源:igfitidea点击:

Spring Boot - How to get all request params in a map in Spring RestController?

javaspringspring-boot

提问by Mourad Karim

Sample URL:

示例网址:

../get/1?attr1=1&attr2=2&attr3=3

I do not know the names of attr1, att2, and attr3.

我不知道 attr1、att2 和 attr3 的名称。

When I ran this code, I get the size of 'allRequestParams' equals to 1

当我运行此代码时,我得到“allRequestParams”的大小等于1

@RequestMapping(value = "/get/", method = RequestMethod.GET)
public String search(
@RequestParam Map<String,Integer> allRequestParams) {
   System.out.println(allRequestParams.size());
   return "";
}

Is it problem with Spring or I wrote a wrong code. Thank you!

是 Spring 有问题还是我写错了代码。谢谢!

回答by Tiny

You can define a POJO which contains a map.. Something like below:

您可以定义一个包含地图的 POJO.. 如下所示:

@RequestMapping(value = "/get/{searchId}", method = RequestMethod.POST)
public String search(
@PathVariable("searchId") Long searchId,
@RequestParam SearchRequest searchRequest) {
 System.out.println(searchRequest.getParams.size());
 return "";
}

public class SearchRequest {   
private Map<String, String> params;
}

Request Object:

请求对象:

"params":{
     "birthDate": "25.01.2011",
    "lang":"en"       
 }

回答by Sampath

If you are passing the request attributes in the form of query parameters then you can directly get it using HttpServletRequest. Pass in as the parameter to your method and use it like httpServletRequest.getParameterMap(). This will return an immutable java.util.Map of request parameters. Each entry of the map will have the key of type String and value of type String[]. So if you have only one value you can directly access as entry.getValue()[0] would give you the first value. Code looks something like this to access the values.

如果您以查询参数的形式传递请求属性,那么您可以使用 HttpServletRequest 直接获取它。作为参数传入您的方法并像 httpServletRequest.getParameterMap() 一样使用它。这将返回一个不可变的 java.util.Map 请求参数。映射的每个条目都将具有 String 类型的键和 String[] 类型的值。所以如果你只有一个值,你可以直接访问,因为 entry.getValue()[0] 会给你第一个值。代码看起来像这样来访问值。

@RequestMapping(value = "/get/", method = RequestMethod.GET) 
public String search(HttpServletRequest httpServletRequest) {
   Map<String, String[]> requestParameterMap = httpServletRequest.getParameterMap();
   for(String key : requestParameterMap.keySet()){
        System.out.println("Key : "+ key +", Value: "+ requestParameterMap.get(key)[0]);
   }
   return "";
}

Hope this helps !!

希望这可以帮助 !!

回答by Mathieu St-Louis

For your specific example aren't you missing a @PathVariable to access Path variable with value "1" in your example URL?

对于您的具体示例,您是否缺少 @PathVariable 来访问示例 URL 中值为“1”的 Path 变量?

@RequestMapping(value = "/get/{searchId}", method = RequestMethod.GET)
public String search(
@PathVariable("searchId") Long searchId,
@RequestParam Map<String,String> allRequestParams) {
   System.out.println(allRequestParams.size());
   return "";
}

Also, are you importing java.util.Map?

另外,您是否正在导入 java.util.Map?