דוגמה להערה Spring MVC @RequestMapping עם קונטרולר, שיטות, כותרות, פרמטרים, @RequestParam, @PathVariable

@RequestMapping היא אחת האנוטציות הנפוצות ביותר בשימוש Spring MVC. האנוטציה org.springframework.web.bind.annotation.RequestMapping משמשת למיפוי של בקשות האינטרנט למחלקות טפל ו/או שיטות טפל מסוימות. אפשר להחיל את @RequestMapping על מחלקת הבקרה וגם על השיטות. היום נבחן את השימושים השונים באנוטציה זו עם דוגמא ואנוטציות אחרות @PathVariable ו־@RequestParam.

Spring @RequestMapping

  1. @RequestMapping עם מחלקה: ניתן להשתמש בו עם הגדרת מחלקה כדי ליצור את URI הבסיסי. לדוגמה:

    @Controller
    @RequestMapping("/home")
    public class HomeController {
    
    }
    

    כעת /home הוא ה־URI שבו ישמש הבקר הזה. העקרון הזה דומה מאוד להקשר של ה־servlet באפליקציית האינטרנט.

  2. @הבהרתMapping עם שיטה: אנו יכולים להשתמש בזה עם שיטה כדי לספק את דפוס ה-URI שבו תשמש שיטת המטפל. לדוגמה:

    @הבהרתMapping(value="/method0")
    @ResponseBody
    public String method0(){
    	return "method0";
    }
    

    ההערה למעלה יכולה גם להיות כתובה כך: @הבהרתMapping("/method0"). לציין בצד זה, אני משתמש ב־@ResponseBody כדי לשלוח את התגובה מסוג String עבור בקשת האינטרנט הזו, זה נעשה כדי לשמור על הדוגמה פשוטה. כמו שאני עושה תמיד, אני אשתמש בשיטות אלה באפליקציית Spring MVC ואבדוק אותן עם תוכנית או סקריפט פשוטים.

  3. @RequestMapping עם מספר URI מרובים: אנו יכולים להשתמש בשיטה אחת לטיפול במספר URI מרובים, לדוגמה:

    @RequestMapping(value={"/method1","/method1/second"})
    @ResponseBody
    public String method1(){
    	return "method1";
    }
    

    אם תסתכלו על קוד המקור של ההערה @RequestMapping, תראו שכל המשתנים שלה הם מערכים. אנו יכולים ליצור מערך String עבור מיפוי ה־URI לשיטת הטיפול.

  4. @בקשהמתוךהערך עם HTTP Method: לפעמים אנו רוצים לבצע פעולות שונות בהתאם לשיטת ה-HTTP המשמשת, אף על פי שמזהה הבקשה נשאר זהה. ניתן להשתמש במשתנה השיטה של @בקשהמתוךהערך כדי להגביל את שימוש השיטה הזו לשיטות ה-HTTP שבשבילן תתבצע הקריאה לשיטה. לדוגמה:

    @בקשהמתוךהערך(ערך="/שיטה2", שיטה=RequestMethod.POST)
    @ResponseBody
    public String שיטה2(){
    	return "שיטה2";
    }
    	
    @בקשהמתוךהערך(ערך="/שיטה3", שיטה={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String שיטה3(){
    	return "שיטה3";
    }
    
  5. @בקשהמתוךהערך עם כותרות: ניתן לציין את הכותרות שצריכות להיות נוכחות כדי לקרוא לשיטת המטפל. לדוגמה:

    @בקשהמתוךהערך(ערך="/שיטה4", כותרות="שם=פנקאג'")
    @ResponseBody
    public String שיטה4(){
    	return "שיטה4";
    }
    	
    @בקשהמתוךהערך(ערך="/שיטה5", כותרות={"שם=פנקאג'", "מזהה=1"})
    @ResponseBody
    public String שיטה5(){
    	return "שיטה5";
    }
    
  6. @RequestMapping עם Produces ו-Consumes: אנו יכולים להשתמש בכותרת Content-Type ו-Accept כדי לקבוע את תוכן הבקשה ואיזו הודעת MIME היא רוצה כתגובה. למען הבהרה, @RequestMapping מספק משתנים produces ו־consumes שבהם אנו יכולים לציין את סוג תוכן הבקשה שבשבילו תתקבל השיטה ואת סוג תוכן התגובה. לדוגמה:

    @RequestMapping(value="/method6", produces={"application/json","application/xml"}, consumes="text/html")
    @ResponseBody
    public String method6(){
    	return "method6";
    }
    

    השיטה לעיל יכולה לקבל הודעה רק עם Content-Type כטקסט/HTML ויכולה לייצר הודעות מסוג application/json ו־application/xml.

קיץ @נתיבעלפי

  1. @RequestMapping עם @PathVariable: האנוטציה RequestMapping ניתנת לשימוש כדי לטפל ב-URIs דינמיים שבהם ערך אחד או יותר של ה-URI פועל כפרמטר. ניתן אף לציין ביטוי רגולרי עבור פרמטר דינמי של ה-URI כדי לקבוע שרק סוג מסוים של קלט יתקבל. זה עובד עם האנוטציה @PathVariable דרך ה-PathVariable שבאמצעותה אנו יכולים למפות את משתנה ה-URI לאחד מארגומנטי השיטה. לדוגמה:

    @RequestMapping(value="/method7/{id}")
    @ResponseBody
    public String method7(@PathVariable("id") int id){
    	return "method7 עם id="+id;
    }
    	
    @RequestMapping(value="/method8/{id:[\\d]+}/{name}")
    @ResponseBody
    public String method8(@PathVariable("id") long id, @PathVariable("name") String name){
    	return "method8 עם id= "+id+" ושם="+name;
    }
    

Spring @RequestParam

  1. @RequestMapping עם @RequestParam עבור פרמטרי URL: לפעמים אנו מקבלים פרמטרים ב-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.
  1. @בקשתמופע שיטת ברירת מחדל: אם הערך ריק עבור שיטה, זה עובד כשיטת ברירת מחדל עבור מחלקת הבקר. לדוגמה:
```
@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.
  1. @בקשתמופע שיטת פילבק: ניתן ליצור שיטת פילבק עבור מחלקת הבקר כדי לוודא שאנו תופסים את כל בקשות הלקוח, גם אם אין שיטות טפס תואמות. זה שימושי בשליחת דפי תגובה מותאמים אישית במקרה של אין שיטות טפס תואמות.
```
@RequestMapping("*")
@ResponseBody
public String fallbackMethod(){
	return "fallback method";
}
```

תוכנית מבחן Spring 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:pankaj" https://localhost:9090/SpringRequestMappingExample/home/method4";
curl -H "name:pankaj" https://localhost:9090/SpringRequestMappingExample/home/method4;
printf "\n\n*****\n\n";

echo "curl -H "name:pankaj" -H "id:1" https://localhost:9090/SpringRequestMappingExample/home/method5";
curl -H "name:pankaj" -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 הוא ההקשר של הסרווילט של האפליקציה. כעת, כאשר אני מפעיל תסריט זה דרך שורת הפקודה, אני מקבל את הפלט הבא.

pankaj:~ pankaj$ ./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:pankaj https://localhost:9090/SpringRequestMappingExample/home/method4
method4

*****

curl -H name:pankaj -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*****

pankaj:~ pankaj$ 

רובם מובנים באופן עצמאי, אף על פי שעשויים להיות שימושים בדיפולט ובשיטות חילופיות. זהו הכל בשביל דוגמה לבקשת Spring RequestMapping, אני מקווה שזה יסייע לך להבין את האנוטציה הזו ואת התכונות השונות שלה. עליך להוריד את פרויקט הדוגמה מהקישור למטה ולנסות תרחישים שונים כדי לחקור אותו עוד יותר.

הורדת פרויקט Spring MVC RequestMapping

Source:
https://www.digitalocean.com/community/tutorials/spring-requestmapping-requestparam-pathvariable-example