Spring MVC: What happens when `@ RequestParam` is omitted

environment

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
	
}

Basic type such as method argument type is String, int

Specify 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.

Do not specify @ 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.

Method argument type is Map <String, String>

Grant @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}.

Do not grant @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.

Two arguments with @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}.

Method argument type is MultiValueMap <String, String>

Grant @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.

Method argument type is Map <String, Object>

Grant @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}.

Bean class whose method argument type is self-made

Do not grant @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;
	}    

Reference site

http://www.ne.jp/asahi/hishidama/home/tech/java/spring/boot/RequestParam.html

Recommended Posts

Spring MVC: What happens when `@ RequestParam` is omitted
What is Spring Tools 4
What is MVC model?
What is @Autowired in Spring boot?
What is the Java Servlet / JSP MVC model?
The official name of Spring MVC is Spring Web MVC
What is Docker?
What is null? ]
What is java
What is Keycloak
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
When Spring Batch is executed continuously in Oracle, ORA-08177
ParseException: What to do when Unparseable date is reached