Description of @RequestParam is as follows It is listed.
If the method parameter is Map<String, String> or MultiValueMap<String, String> and a parameter name is not specified, then the map parameter is populated with all request parameter names and values.
I actually moved and confirmed that this was correct.
SampleController
@Controller
public class SampleController {
//Add method here
}
name
attribute by giving @RequestParam
SampleController.java
@GetMapping("/search1")
@ResponseBody
String search1(@RequestParam(name="name") String bar,
@RequestParam(name="id") int hoge ) {
String result = "name="+ bar + ",id=" + hoge;
return result;
}
When I accessed / search1? name = test & id = 2
, I got name = test, id = 2
.
@ RequestParam
@GetMapping("/search2")
@ResponseBody
String search2(String name, int id ) {
String result = "name="+name + ",id=" + id;
return result;
}
When I accessed / search1? name = test & id = 2
, I got name = test, id = 2
.
Map <String, String>
@RequestParam
@GetMapping("/search6")
@ResponseBody
String search6(@RequestParam Map<String, String> params) {
String result = params.toString();
return result;
}
When I accessed / search6? name = test & id = 2
, I got{name = test, id = 2}
.
@RequestParam
@GetMapping("/search5")
@ResponseBody
String search5(Map<String, String> params) {
String result = params.toString();
return result;
}
When I accessed / search5? name = test & id = 2
,{}
was displayed.
@RequestParam
@GetMapping("/search9")
@ResponseBody
String search6(@RequestParam Map<String, String> params, @RequestParam Map<String, String> params2) {
String result = params.toString() + "," + params2.toString();
return result;
}
When I accessed / search9? name = test & id = 2
, I got{name = test, id = 2}, {name = test, id = 2}
.
MultiValueMap <String, String>
@RequestParam
@GetMapping("/search8")
@ResponseBody
String search8(@RequestParam MultiValueMap<String, String> params) {
String result = params.toString();
return result;
}
When I accessed / search8? name = test & id = 2 & name = sample
,{name = [test, sample], id = [2]}
was displayed.
Map <String, Object>
@RequestParam
@GetMapping("/search4")
@ResponseBody
String search4(@RequestParam Map<String, Object> params) {
String result = params.toString();
return result;
}
When I accessed / search4? name = test & id = 2
, I got{name = test, id = 2}
.
@RequestParam
@GetMapping("/search3")
@ResponseBody
String search3(SampleBean params) {
String result = "name="+params.getName() + ",id=" + params.getId();
return result;
}
@Data //lombok annotation
public static class SampleBean {
String name;
int id;
}
http://www.ne.jp/asahi/hishidama/home/tech/java/spring/boot/RequestParam.html
Recommended Posts