Video: Qhov txawv ntawm @RequestMapping thiab @PostMapping yog dab tsi?
2024 Tus sau: Lynn Donovan | [email protected]. Kawg hloov kho: 2023-12-15 23:47
Los ntawm lub rooj sib tham naming peb tuaj yeem pom tias txhua cov lus piav qhia yog txhais tau los ua raws li hom kev thov nkag los, piv txwv li @GetMapping yog siv los lis hom GET ntawm thov txoj kev, @ PostMapping yog siv los lis POST hom ntawm thov txoj kev, thiab lwm yam.
Hauv qhov no, qhov txawv ntawm @RequestMapping thiab @PostMapping yog dab tsi?
Tshwj xeeb, @ PostMapping yog ib qho annotation uas ua raws li qhov shortcut rau @ ThovMapping (txoj kev = RequestMethod. Yog li nws tsuas yog yooj yim annotation uas yog ntau "verbose" thiab qhia tias txoj kev piav qhia nrog nws siv rau tuav POST HTTP thov. Kuv nyuam qhuav tshawb xyuas koj txoj kev tswj hwm nrog 2.1.
Ib sab saum toj, GetMapping thiab PostMapping yog dab tsi? @ GetMapping yog tshwj xeeb version ntawm @RequestMapping annotation uas ua raws li ib tug shortcut rau @RequestMapping (txoj kev = RequestMethod. GET). @ GetMapping cov txheej txheem sau tseg ua haujlwm rau HTTP GET cov lus thov sib tw nrog cov lus qhia URI.
Hais txog qhov no, PostMapping yog dab tsi?
@ PostMapping yog ib qho annotation uas ua raws li qhov shortcut rau @RequestMapping(txoj kev = RequestMethod. POST). @ PostMapping cov lus piav qhia txog kev tswj hwm HTTP POST cov lus thov sib phim nrog cov lus qhia URI. Cov lus piav qhia no tuaj yeem txhim kho kev nyeem tau ntawm cov lej.
Vim li cas peb thiaj siv @PostMapping?
@ PostMapping los lis HTTP POST Thov Daim Ntawv Ceeb Toom tias txoj kev lav phib xaub rau kev tuav HTTP POST thov yuav tsum tau sau tseg nrog @ PostMapping lus piav qhia. Daim ntawv ceeb toom yuav ua li cas @RequestBody annotation yog siv txhawm rau khij cov qauv kev sib cav uas cov ntaub ntawv JSON yuav raug hloov los ntawm Spring Framework.