12
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring MVC:`@RequestParam`の省略したときの動き

Last updated at Posted at 2018-03-25

環境

  • Spring Boot 1.5.9
    • Spring MVC

@RequestParamの説明 には、以下のように記載されています。

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.

これが正しいことを、実際に動かして確認しました。

SampleController

@Controller
public class SampleController {
//ここにメソッドを追加
	
}

メソッド引数の型がString, intなど基本的な型

@RequestParamを付与してname属性を指定

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;
	}

/search1?name=test&id=2にアクセスすると、name=test,id=2が表示されました。

@RequestParamを指定しない

	@GetMapping("/search2")
	@ResponseBody
	String search2(String name, int id ) {
		String result = "name="+name + ",id=" + id;
		return result;
	}

/search1?name=test&id=2にアクセスすると、name=test,id=2が表示されました。

メソッド引数の型がMap<String, String>

@RequestParamを付与する

	@GetMapping("/search6")
	@ResponseBody
	String search6(@RequestParam Map<String, String> params) {
		String result = params.toString();
		return result;
	}

/search6?name=test&id=2にアクセスすると、{name=test, id=2}が表示されました。

@RequestParamを付与しない

	@GetMapping("/search5")
	@ResponseBody
	String search5(Map<String, String> params) {
		String result = params.toString();
		return result;
	}

/search5?name=test&id=2にアクセスすると、{}が表示されました。

@RequestParamを付与した引数が2個

	@GetMapping("/search9")
	@ResponseBody
	String search6(@RequestParam Map<String, String> params, @RequestParam Map<String, String> params2) {
		String result = params.toString() + "," + params2.toString();
		return result;
	}

/search9?name=test&id=2にアクセスすると、{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;
	}

/search8?name=test&id=2&name=sampleにアクセスすると、{name=[test, sample], id=[2]}が表示されました。

メソッド引数の型がMap<String, Object>

@RequestParamを付与する

	@GetMapping("/search4")
	@ResponseBody
	String search4(@RequestParam Map<String, Object> params) {
		String result = params.toString();
		return result;
	}

/search4?name=test&id=2にアクセスすると、{name=test, id=2}が表示されました。

メソッド引数の型が自作のBeanクラス

@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;
	}    

参考サイト

12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?