@RequestMapping هي واحدة من أكثر التعليمات استخدامًا في Spring MVC. تُستخدم تعليمة org.springframework.web.bind.annotation.RequestMapping
لتعيين طلبات الويب على فئات المعالج الخاصة و/أو أساليب المعالج. يمكن تطبيق
@RequestMapping
على فئة التحكم بالإضافة إلى الأساليب. اليوم سننظر في استخدامات مختلفة لهذا التعليق مع أمثلة وتعليقات أخرى @PathVariable
و @RequestParam
.
Spring @RequestMapping
-
@RequestMapping مع الفئة: يمكننا استخدامه مع تعريف الفئة لإنشاء نطاق URI الأساسي. على سبيل المثال:
@Controller @RequestMapping("/home") public class HomeController { }
الآن /home هو URI الذي سيتم استخدامه لهذا التحكم. هذا المفهوم مشابه لسياق سيرفلت في تطبيق الويب.
-
@RequestMapping مع الطريقة: يمكننا استخدامه مع الطريقة لتوفير نمط URI الذي سيتم استخدام طريقة المعالج له. على سبيل المثال:
@RequestMapping(value="/method0") @ResponseBody public String method0(){ return "method0"; }
يمكن كتابة التعليق أعلاه أيضًا كـ
@RequestMapping("/method0")
. على جانب ملاحظة، أنا استخدم @ResponseBody لإرسال استجابة النص لهذا الطلب عبر الويب، يتم ذلك لإبقاء المثال بسيطًا. كما أفعل دائمًا، سأستخدم هذه الطرق في تطبيق Spring MVC واختبارها باستخدام برنامج أو سكريبت بسيط. -
@RequestMapping بعدة روابط: يمكننا استخدام طريقة واحدة للتعامل مع عدة روابط، على سبيل المثال:
@RequestMapping(value={"/method1","/method1/second"}) @ResponseBody public String method1(){ return "method1"; }
إذا نظرت إلى الشيفرة المصدرية للتعليق RequestMapping، سترى أن جميع متغيراتها هي مصفوفات. يمكننا إنشاء مصفوفة سلسلة نصية لتعيين روابط URI لطريقة المعالج.
-
@RequestMapping بطريقة HTTP: في بعض الأحيان نريد أداء عمليات مختلفة استنادًا إلى الطريقة التي تم استخدامها في HTTP ، حتى وإن كان معرف الطلب هو نفسه. يمكننا استخدام المتغير @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=pankaj") @ResponseBody public String method4(){ return "method4"; } @RequestMapping(value="/method5", headers={"name=pankaj", "id=1"}) @ResponseBody public String method5(){ return "method5"; }
-
@RequestMapping بميزات Produces و Consumes: يمكننا استخدام رأس
Content-Type
وAccept
لمعرفة محتويات الطلب ونوع الرسالة الميمية التي يرغب فيها. للوضوح، يوفر @RequestMapping متغيرات produces و consumes حيث يمكننا تحديد نوع محتوى الطلب الذي سيتم استدعاء الطريقة به ونوع محتوى الرد. على سبيل المثال:@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
-
@RequestMapping مع @PathVariable: يمكن استخدام تعليمة الطلب (RequestMapping annotation) للتعامل مع عناوين URI الديناميكية حيث يعمل أحد قيم الـ URI كمعلمة أو أكثر. يمكننا حتى تحديد تعبيرات منتظمة لمعلمة URI الديناميكية لقبول نوع محدد من المدخلات فقط. يعمل ذلك بالتعليمة @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
- @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.
- @RequestMappingالأسلوب الافتراضي: إذا كانت القيمة فارغة لطريقة، فإنه يعمل كأسلوب افتراضي لفئة المتحكم. على سبيل المثال:
```
@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";
}
```
برنامج اختبار 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$
معظم هذه الأمور مفهومة ذاتيًا، على الرغم من أنه قد ترغب في التحقق من الطرق الافتراضية والبديلة. هذا كل شيء بالنسبة لـ مثال طلب الربيع، آمل أن يساعدك ذلك في فهم هذه التعليقة وميزاتها المختلفة. يجب عليك تنزيل المشروع التوضيحي من الرابط أدناه وتجربة سيناريوهات مختلفة لاستكشافه بشكل أعمق.