Spring MVCの@RequestMappingアノテーションの例。コントローラー、メソッド、ヘッダー、パラメーター、@RequestParam、@PathVariableの使用例。

@RequestMappingは、最も広く使用されているSpring MVCのアノテーションの1つです。org.springframework.web.bind.annotation.RequestMappingアノテーションは、Webリクエストを特定のハンドラクラスやハンドラメソッドにマッピングするために使用されます。@RequestMappingは、コントローラクラスだけでなく、メソッドにも適用することができます。今日は、このアノテーションのさまざまな使用方法と、例や他のアノテーションである@PathVariableと@RequestParamについて説明します。

春の @RequestMapping

    1. 「@RequestMapping」を使った場合のクラスとメソッドの使い方は以下の通りです:

クラスでの「@RequestMapping」の使用:クラスの定義と一緒に使用することで、ベースのURIを作成することができます。例えば:
@Controller
@RequestMapping(“/home”)
public class HomeController {

}

上記の場合、「/home」はこのコントローラーに使用されるURIです。この概念は、Webアプリケーションのサーブレットコンテキストと非常によく似ています。

メソッドでの「@RequestMapping」の使用:メソッドと一緒に使用することで、ハンドラメソッドに対するURIパターンを指定することができます。例えば:
@RequestMapping(value=”/method0″)
@ResponseBody
public String method0(){
return “method0”;
}

上記のアノテーションは、「@RequestMapping(“/method0”)」としても書くことができます。ちなみに、このWebリクエストのためにStringのレスポンスを送信するために「@ResponseBody」を使用しています。これは、例をシンプルにするためです。私がいつもやるように、これらのメソッドをSpring MVCアプリケーションで使用し、単純なプログラムやスクリプトでテストします。

複数のURIでの「@RequestMapping」の使用:複数のURIの処理に単一のメソッドを使用することができます。例えば:
@RequestMapping(value={“/method1″,”/method1/second”})
@ResponseBody
public String method1(){
return “method1″;
}

「RequestMapping」アノテーションのソースコードを見ると、すべての変数が配列であることがわかります。ハンドラメソッドのURIマッピングのためにString配列を作成することができます。

HTTPメソッドでの「@RequestMapping」の使用:リクエストURIは同じでも、異なる操作を行いたい場合があります。この場合、「@RequestMapping」のメソッド変数を使用して、このメソッドが呼び出されるHTTPメソッドを絞り込むことができます。例えば:
@RequestMapping(value=”/method2”, method=RequestMethod.POST)
@ResponseBody
public String method2(){
return “method2″;
}

@RequestMapping(value=”/method3”, method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public String method3(){
return “method3″;
}

ヘッダーでの「@RequestMapping」の使用:ハンドラメソッドを呼び出すために必要なヘッダーを指定することができます。例えば:
@RequestMapping(value=”/method4″, headers=”name=scdev”)
@ResponseBody
public String method4(){
return “method4″;
}

@RequestMapping(value=”/method5”, headers={“name=scdev”, “id=1”})
@ResponseBody
public String method5(){
return “method5″;
}

プロデュースとコンシュームのための「@RequestMapping」:リクエストの形式やレスポンスのMIMEタイプを特定するために、Content-TypeヘッダーとAcceptヘッダーを使用することがあります。明確さのために、「@RequestMapping」では、プロデュースとコンシュームの変数を提供し、メソッドが呼び出されるリクエストのコンテンツタイプとレスポンスのコンテンツタイプを指定することができます。例えば:
@RequestMapping(value=”/method6”, produces={“application/json”,”application/xml”}, consumes=”text/html”)
@ResponseBody
public String method6(){
return “method6”;
}

上記のメソッドは、Content-Typeをtext/htmlとしてメッセージを消費し、application/jsonおよびapplication/xmlのタイプのメッセージを生成することができます。

春の @PathVariable

    1. @RequestMappingと@PathVariable:RequestMappingアノテーションは、URIの1つまたは複数の値がパラメータとして機能する動的URIを処理するために使用できます。URIの動的パラメータには、特定の入力タイプのみを受け入れるための正規表現を指定することもできます。@PathVariableアノテーションと一緒に機能し、URI変数をメソッドの引数の1つにマップすることができます。例えば:

@RequestMapping(value=”/method7/{id}”)
@ResponseBody
public String method7(@PathVariable(“id”) int id){
return “method7 with id=”+id;
}

@RequestMapping(value=”/method8/{id:[\\d]+}/{name}”)
@ResponseBody
public String method8(@PathVariable(“id”) long id, @PathVariable(“name”) String name){
return “method8 with id= “+id+” and name=”+name;
}

リクエストパラメーターのSpringにおけるSpring @RequestParam

    URLパラメーターに対する@RequestParamを使用する@RequestMapping:リクエストURLでパラメーターを取得することがあります。主にGETリクエストで使用されます。@RequestMappingアノテーションと@RequestParamアノテーションを使用して、URLパラメーターを取得し、メソッド引数にマップすることができます。例えば:
```
@RequestMapping(value="/method9")
@ResponseBody
public String method9(@RequestParam("id") int id){
	return "method9 with id= "+id;
}
```

For this method to work, the parameter name should be "id" and it should be of type int.
    @RequestMappingのデフォルトメソッド:メソッドのvalueが空の場合、それはコントローラークラスのデフォルトメソッドとして機能します。例えば:
```
@RequestMapping()
@ResponseBody
public String defaultMethod(){
	return "default method";
}
```

As you have seen above that we have mapped `/home` to `HomeController`, this method will be used for the default URI requests.
    @RequestMappingのフォールバックメソッド: フォールバックメソッドをコントローラークラスに作成することで、マッチングするハンドラーメソッドがないときでも、すべてのクライアントリクエストをキャッチできます。リクエストに対するハンドラーメソッドが存在しない場合、カスタムの404レスポンスページをユーザーに送信する際に便利です。
```
@RequestMapping("*")
@ResponseBody
public String fallbackMethod(){
	return "fallback method";
}
```

春のRestMappingテストプログラム

上記の異なるメソッドをテストするために、Spring RestTemplateを使用できますが、今日はこれらのメソッドをテストするためにcURLコマンドを使用します。これらはシンプルでデータの流れも少ないためです。上記のすべてのメソッドを呼び出し、出力を表示するための簡単なシェルスクリプトspringTest.shを作成しました。以下のように見えます。

#!/bin/bash

echo "curl https://localhost:9090/SpringRequestMappingExample/home/method0";
curl https://localhost:9090/SpringRequestMappingExample/home/method0;
printf "\n\n*****\n\n";

echo "curl https://localhost:9090/SpringRequestMappingExample/home";
curl https://localhost:9090/SpringRequestMappingExample/home;
printf "\n\n*****\n\n";

echo "curl https://localhost:9090/SpringRequestMappingExample/home/xyz";
curl https://localhost:9090/SpringRequestMappingExample/home/xyz;
printf "\n\n*****\n\n";

echo "curl https://localhost:9090/SpringRequestMappingExample/home/method1";
curl https://localhost:9090/SpringRequestMappingExample/home/method1;
printf "\n\n*****\n\n";

echo "curl https://localhost:9090/SpringRequestMappingExample/home/method1/second";
curl https://localhost:9090/SpringRequestMappingExample/home/method1/second;
printf "\n\n*****\n\n";

echo "curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method2";
curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method2;
printf "\n\n*****\n\n";

echo "curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method3";
curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method3;
printf "\n\n*****\n\n";

echo "curl -X GET https://localhost:9090/SpringRequestMappingExample/home/method3";
curl -X GET https://localhost:9090/SpringRequestMappingExample/home/method3;
printf "\n\n*****\n\n";

echo "curl -H "name:scdev" https://localhost:9090/SpringRequestMappingExample/home/method4";
curl -H "name:scdev" https://localhost:9090/SpringRequestMappingExample/home/method4;
printf "\n\n*****\n\n";

echo "curl -H "name:scdev" -H "id:1" https://localhost:9090/SpringRequestMappingExample/home/method5";
curl -H "name:scdev" -H "id:1" https://localhost:9090/SpringRequestMappingExample/home/method5;
printf "\n\n*****\n\n";

echo "curl -H "Content-Type:text/html" https://localhost:9090/SpringRequestMappingExample/home/method6";
curl -H "Content-Type:text/html" https://localhost:9090/SpringRequestMappingExample/home/method6;
printf "\n\n*****\n\n";

echo "curl https://localhost:9090/SpringRequestMappingExample/home/method6";
curl https://localhost:9090/SpringRequestMappingExample/home/method6;
printf "\n\n*****\n\n";

echo "curl -H "Content-Type:text/html" -H "Accept:application/json" -i https://localhost:9090/SpringRequestMappingExample/home/method6";
curl -H "Content-Type:text/html" -H "Accept:application/json" -i https://localhost:9090/SpringRequestMappingExample/home/method6;
printf "\n\n*****\n\n";

echo "curl -H "Content-Type:text/html" -H "Accept:application/xml" -i https://localhost:9090/SpringRequestMappingExample/home/method6";
curl -H "Content-Type:text/html" -H "Accept:application/xml" -i https://localhost:9090/SpringRequestMappingExample/home/method6;
printf "\n\n*****\n\n";

echo "curl https://localhost:9090/SpringRequestMappingExample/home/method7/1";
curl https://localhost:9090/SpringRequestMappingExample/home/method7/1;
printf "\n\n*****\n\n";

echo "curl https://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa";
curl https://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa;
printf "\n\n*****\n\n";

echo "curl https://localhost:9090/SpringRequestMappingExample/home/method9?id=20";
curl https://localhost:9090/SpringRequestMappingExample/home/method9?id=20;
printf "\n\n*****DONE*****\n\n";

私のウェブアプリケーションはTomcat-7に展開され、ポート9090で実行されています。SpringRequestMappingExampleはアプリケーションのサーブレットコンテキストです。これをコマンドラインで実行すると、以下の出力が得られます。

scdev:~ scdev$ ./springTest.sh 
curl https://localhost:9090/SpringRequestMappingExample/home/method0
method0

*****

curl https://localhost:9090/SpringRequestMappingExample/home
default method

*****

curl https://localhost:9090/SpringRequestMappingExample/home/xyz
fallback method

*****

curl https://localhost:9090/SpringRequestMappingExample/home/method1
method1

*****

curl https://localhost:9090/SpringRequestMappingExample/home/method1/second
method1

*****

curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method2
method2

*****

curl -X POST https://localhost:9090/SpringRequestMappingExample/home/method3
method3

*****

curl -X GET https://localhost:9090/SpringRequestMappingExample/home/method3
method3

*****

curl -H name:scdev https://localhost:9090/SpringRequestMappingExample/home/method4
method4

*****

curl -H name:scdev -H id:1 https://localhost:9090/SpringRequestMappingExample/home/method5
method5

*****

curl -H Content-Type:text/html https://localhost:9090/SpringRequestMappingExample/home/method6
method6

*****

curl https://localhost:9090/SpringRequestMappingExample/home/method6
fallback method

*****

curl -H Content-Type:text/html -H Accept:application/json -i https://localhost:9090/SpringRequestMappingExample/home/method6
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Content-Length: 7
Date: Thu, 03 Jul 2014 18:14:10 GMT

method6

*****

curl -H Content-Type:text/html -H Accept:application/xml -i https://localhost:9090/SpringRequestMappingExample/home/method6
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/xml
Content-Length: 7
Date: Thu, 03 Jul 2014 18:14:10 GMT

method6

*****

curl https://localhost:9090/SpringRequestMappingExample/home/method7/1
method7 with id=1

*****

curl https://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa
method8 with id= 10 and name=Lisa

*****

curl https://localhost:9090/SpringRequestMappingExample/home/method9?id=20
method9 with id= 20

*****DONE*****

scdev:~ scdev$ 

これらのほとんどは自己に理解できるものですが、デフォルトやフォールバックの方法を確認したいかもしれません。これがSpring RequestMappingの例です。この注釈とその様々な機能を理解するのに役立つと思います。以下のリンクからサンプルプロジェクトをダウンロードして、さまざまなシナリオを試してさらに探求してみてください。

Spring MVCのRequestMappingプロジェクトをダウンロードしてください。

コメントを残す 0

Your email address will not be published. Required fields are marked *