Spring Supplements (3) - Solving the problem of receiving request parameters with map

When I encounter a cross-domain call, because the parameters I pass are uncertain, I need to receive the parameters through map and perform operations such as signature verification.


As a matter of course, I wrote the following code, but I found that the key-value value was not obtained in the map.

[java] view plain copy
  1. @RequestMapping(value = "/callback", produces = "text/html;charset=UTF-8")  
  2. @ResponseBody  
  3. public String callback(@RequestBody Map<String, String> params) {  
  4.     returnnull;   
  5. }  

After that, I found that HttpServletRequest has a getParameterMap method that seems to be awesome
[java] view plain copy
  1. @RequestMapping(value = "/callback", produces = "text/html;charset=UTF-8")  
  2. @ResponseBody  
  3. public String callback(HttpServletRequest httpServletRequest) {  
  4.     Map<String, String> params = httpServletRequest.getParameterMap();  
  5.     returnnull;   
  6. }  

However, it is still wrong to write this, because this method actually returns a Map<String, String[]> object, not the simple Map<String, String> type I imagined. Here, the correct parameters can be obtained, but It needs to be converted manually, it can't be the case, there must be a better solution

At this time, the master said to me "try it with @RequestParam", and it was right. . correct. . . .

[java] view plain copy
  1. @RequestMapping(value = "/callback", produces = "text/html;charset=UTF-8")  
  2. @ResponseBody  
  3. public String callback(@RequestParam Map<String, String> params) {  
  4.     returnnull;   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326370419&siteId=291194637
Recommended