REST ºÍ HTTP ÓïÒå
roy fielding ½¨ÉèÁË rest ×÷ΪËûµÄ²©Ê¿ÂÛÎÄ¡£
¶ÁÍêºó£¬ÎһὫÆä¹é½áΪÈý¸ö»ù±¾ÒªËØ£º
ÐÎò¹¤¾ß״̬µÄÎĵµ
ÔÚϵͳ֮¼äÍù·µ´«Ê乤¾ß״̬µÄ´«Êä»úÖÆ
¶Ô״ִ̬ÐеÄÒ»×é²Ù×÷
ËäÈ» Roy ֻרעÓÚ HTTP£¬µ«ÎÒ²»Ã÷ȷΪʲô²»¿ÉʹÓÃÆäËû´«Êä·½·¨¡£ÒÔÏÂÊÇһЩʾÀý£º
×°Öà WebDAV ¹²Ïí£¨WebDAV ÊÇ HTTP À©Õ¹£¬Òò´ËÈÔʹÓà HTTP£©¡£½«µç×Ó±í¸ñ£¨.xls¡¢.xlsx¡¢.csv¡¢.ods£©¸´ÖƵ½ÒÑ×°ÖõÄÎļþ¼ÐÖУ¬ÆäÖÐÿһÐж¼ÊÇеÄ/¸üеÄ״̬¡£¸´ÖƵ½¹²ÏíµÄÐÐΪÌåÏÖ¸üвåÈë²Ù×÷£¬ÎļþÃûÌåÏÖÊý¾ÝÀàÐÍ£¬ÁÐÊÇ×ֶΡ£Ð§ÀÍÆ÷ÒÔ£¨ÎĵµÃû³Æ£©-status.£¨Îĵµºó׺£©¾ÙÐÐÏìÓ¦£¬ËüÌṩÿÐеļü¡¢×´Ì¬ÒÔ¼°¿ÉÄܵĹýʧÐÂÎÅ¡£ÔÚÕâÖÖÇéÐÎÏ£¬ÇëÇóÊý¾Ý²¢Ã»Óжà´óÒâÒå¡£
ʹÓà gRPC¡£´«ÊäµÄ¹¤¾ßÊÇÎĵµ£¬HTTP ÊÇ´«Ê䣬Զ³ÌÒªÁìµÄÃû³ÆÊDzÙ×÷¡£Êý¾Ý¿ÉÒÔÌṩºÍÇëÇó¡£
ʹÓà FTP¡£ Óë WebDAV ÀàËÆ£¬ËüÊÇ»ùÓÚÎļþµÄ¡£ PUT ÏÂÁîÊǸüвåÈ룬GET ÏÂÁîÊÇÇëÇó¡£ GET ½öÌṩÎļþÃû£¬Òò´ËËüͨ³£Ìṩָ¶¨ÀàÐ͵ÄËùÓÐÊý¾Ý¡£¿ÉÒÔÔÊÐíʹÓÃÌØÊâÎļþÃûÀ´Ö¸Ê¾Ó²±àÂë¹ýÂËÆ÷ÒÔ»ñÈ¡Êý¾Ý×Ó¼¯¡£
ÿµ±ÎÒÔÚÒ°Íâ¿´µ½ REST ʵÏÖʱ£¬ËüÃÇͨ³£²»×ñÕÕ»ù±¾µÄ HTTPÓïÒåѧ£¬ÎÒ´Óδ¼û¹ý¶Ô´Ë¸ø³öÈκÎÚ¹ÊÍ£¬Ö»ÊÇÒ»¶Ñ²î±ðµÄÒâ¼û¡£ÎÒ·¢Ã÷ÕâЩ¶¼Ã»ÓÐÒýÓà RFC¡£´ó´ó¶¼ÈËËƺõÒÔΪ£º
POST = ½¨Éè
PUT = ¸üÐÂÕû¸öÎĵµ
PATCH = ¸üÐÂÎĵµµÄÒ»²¿·Ö
GET = ¼ìË÷Õû¸öÎĵµ
ÕâÓë HTTP ¹ØÓÚ POST ºÍ PUT µÄ»®¶¨Ïà·´:
PUT ÊÇ¡°½¨É衱»ò¡°¸üС±¡£ GET ͨ³£»á·µ»ØÉÏ´Î PUT µÄÄÚÈÝ¡£ÈôÊÇ PUT ½¨É裬Ëü±ØÐè·µ»Ø 201 Created¡£ÈôÊÇ PUT ¸üУ¬Ëü±ØÐè·µ»Ø 200 OK »ò 204 No Content¡£ RFC ½¨Òé PUT µÄ 200 OK ÄÚÈÝÓ¦¸ÃÊDzÙ×÷µÄ״̬¡£ÎÒÒÔΪ¾Í SQL ¶øÑÔ£¬´Ó select Óï¾ä·µ»ØÐÂÐÐÊÇ¿ÉÒԵġ£ÕâÑù×öµÄÓŵãÊÇ£¬ÈκÎÌìÉúµÄÁж¼»á·µ»Ø¸øŲÓÃÕߣ¬¶øÎÞÐèÖ´Ðе¥¶ÀµÄ GET¡£
POST ƾ֤×ÊÔ´×ÔÉíµÄÓïÒå´¦Àí×ÊÔ´¡£½Ï¾ÉµÄ RFC ÌåÏÖ POST ÊÊÓÃÓÚ×ÊÔ´µÄÏÂÊô¡£ËùÓа汾¶¼¸ø³öÁ˽«ÎÄÕÂÐû²¼µ½ÓʼþÁбíµÄʾÀý£»ËùÓа汾¶¼Ëµ£¬ÈôÊǽ¨ÉèÁË×ÊÔ´£¬ÔòÓ¦·µ»Ø 201 Created¡£
ÎÒÒÔΪ POST µÄÕæÕý¼ÄÒåÊÇ£º
³ý½¨Éè¡¢ËùÓÐ/²¿·Ö¸üлòɾ³ýÖ®ÍâµÄÈκÎÊý¾Ý²Ù×÷
ÈκηÇÊý¾Ý²Ù×÷µÄ²Ù×÷£¬ÀýÈ磺
Ö´ÐÐÈ«ÎÄËÑË÷Óë¶ÌÓïÆ¥ÅäµÄÐС£
ÌìÉúÒªÔÚµØͼÉÏÏÔʾµÄ GIS ¹¤¾ß¡£
¸Ã´Ê±ØÐèÌåÏÖÄúµÄ½öµ±Äúƾ֤»®¶¨Ö´ÐÐʱ£¬ÊµÏÖ²ÅÇÐºÏ HTTP¡£½öʹÓà PUT ¾ÙÐиüÐÂÏÔÈ»²»»áÆÆËðÈκÎÄÚÈÝ£¬Ö»ÊÇÓÉÓÚËü²»ÇÐºÏ RFC ±ê×¼¡£ÈôÊÇÄúÌṩ´¦Àí·¢ËÍ/ÎüÊÕÊý¾ÝµÄËùÓÐϸ½ÚµÄ¿Í»§¶Ë£¬ÄÇôʹÓÃʲô¶¯´Ê¹ØÓÚ¿Í»§¶ËµÄÓû§À´Ëµ²¢²»Ö÷Òª¡£
ÎÒÊÇÄÇÖÖÏëÒªÒ»¸öÀíÓɵÄÈ˲»×ñÕÕ RFC¡£ÎÒÀúÀ´²»Ã÷È·ÔÚ REST API Öн«½¨ÉèÓë¸üÐÂÍÑÀëµÄÖ÷ÒªÐÔ£¬¾ÍÏñÔÚ Web Ó¦ÓóÌÐòÖÐÒ»Ñù¡£ÏëÏëÈÕÀúÔ¼»á¡¢Ìõ¼Ç¡¢ÁªÏµÈ˵ÈÊÖ»úÓ¦ÓóÌÐò£º
¡°½¨É衱µã»÷¼ÓºÅͼ±ê£¬Ëü»áÏÔʾһ¸ö´øÓпÕÖµ»òĬÈÏÖµµÄÐÂ±íµ¥¡£
¡°¸üС±ÕýÔÚÑ¡ÔñÒ»¸ö¹¤¾ß²¢µã»÷Ǧ±Êͼ±ê£¬Õ⽫ÏÔʾ°üÀ¨Ä¿½ñÖµµÄÌõÄ¿±íµ¥¡£
Ò»µ©ÌõÄ¿±íµ¥·ºÆð£¬Ëü¾Í»áÏÔʾÔÚ×Ö¶ÎÑéÖ¤·½ÃæµÄÊÂÇé·½·¨ÍêÈ«Ïàͬ¡£
ÄÇôΪʲô REST API ºÍ Web Ç°¶ËÓëÊÖ»úÓ¦ÓóÌÐòÓÐÈκβî±ðÄØ£¿ÈôÊǵ绰Óû§»ñµÃÓÃÓÚ¡°½¨É衱ºÍ¡°¸üС±µÄÏàͬÊý¾ÝÊäÈë±íµ¥ÓÐ×ÊÖú£¬ÄÇôËü¶Ô API ºÍÍøÂçÓû§ÊÇ·ñҲͬÑùÓÐ×ÊÖú£¿
ÈôÊÇÄú¾öÒéʹÓà PUT ×÷Ϊ¡°½¨É衱»ò¡°¸üС±£¬²¢ÇÒÄúʹÓà SQL ×÷Ϊ´æ´¢£¬´ó´ó¶¼¹©Ó¦É̶¼ÓÐijÖÖÀàÐ͵ĸüвåÈëÅÌÎÊ¡£²»ÐÒµÄÊÇ£¬ÕâÎÞÖúÓÚ¾öÒéºÎʱ·µ»Ø 200 OK »ò 201 Created¡£Äú±ØÐèÉó²éÇý¶¯³ÌÐòÔÚÖ´ÐÐ DML ÅÌÎÊʱÌṩµÄÐÅÏ¢£¬ÒÔÕÒµ½Çø·Ö¸üвåÈëµÄ²åÈëºÍ¸üеÄÒªÁ죬»òʹÓÃÆäËûÅÌÎÊÕ½ÂÔ¡£
Ò»¸ö¼òÆÓµÄʾÀýÊÇÖ´Ðиüм¯…ÆäÖÐ pk ÁÐ = pk Öµ¡£ÈôÊÇÒ»ÐÐÊܵ½Ó°Ï죬Ôò¸ÃÐб£´æ²¢ÒѸüУ»²»È»£¬¸ÃÐв»±£´æ£¬ÐèÒª²åÈë¡£ÔÚ Postgres ÉÏ£¬Äú¿ÉÒÔʹÓà RETURNING ×Ӿ䣬ËüÏÖʵÉÏ¿ÉÒÔ·µ»ØÈκÎÄÚÈÝ£¬¶ø²»µ«½öÊÇÐÐÊý¾Ý£¬ÈçÏÂËùʾ£º
SQL VALUES (…) ON CONFLICT( ) DO UPDATE SET ( …) ·µ»Ø (SELECT COUNT( ) FROMWHERE = ) ±£´æ” data-lang=”text/x-sql” style=”box-sizing: border-box;”>1
INSERT INTO <table> VALUES (...) ON CONFLICT(<pk column>) DO UPDATE SET (...) RETURNING (SELECT COUNT(<pk column>) FROM <table> WHERE <pk column> = <pk value>) exists
µÇ¼ºó¸´ÖÆ
ÕâÑù×öµÄÌì²ÅÔÚÓÚ£º
RETURNING ×Ó¾äÖеÄ×ÓÅÌÎÊÊ×ÏÈÖ´ÐУ¬Òò´ËËü»áÔÚÖ´ÐÐ INSERT ON CONFLICT UPDATE ÅÌÎÊ֮ǰȷ¶¨¸ÃÐÐÊÇ·ñ±£´æ¡£ÅÌÎʵÄЧ¹ûÊÇÃûΪ¡°exists¡±µÄÁУ¬ÈôÊǸÃÐÐÔÚÅÌÎÊ֮ǰ±£´æ£¬Ôò¸ÃÁÐΪ 1¡£Ö´ÐУ¬ÈôÊÇûÓÐÖ´ÐУ¬Ôò·µ»Ø 0¡£
RETURNING ×Ӿ仹¿ÉÒÔ·µ»ØÐеÄÁУ¬°üÀ¨Î´ÌṩµÄÈκÎÌìÉúÄÚÈÝ¡£
ÄúÖ»ÐèŪÇåÎúÈçÄÇÀïÖÃÊÇ·ñÐèÒª²åÈë»ò¸üУ¬²¢¾ÙÐÐÒ»¸ö¼òÆÓµÄÁýͳ£¬ÄúµÄËùÓÐ PUT ¶¼¿ÉÒÔŲÓÃËüÀ´´¦Àí 200 OK »ò 201 Created¡£
ʹÓõÄÒ»¸öºÜºÃµÄÀûÒæPUT µÄÄ¿µÄÊÇ£¬Ò»µ©Äú¿´µ½ POST£¬Äú¾ÍÈ·¶¨Ëü²»ÊǼìË÷»ò³¤ÆÚÐÔ£¬Ïà·´£¬ÄúÖªµÀËÑË÷ POST À´²éÕÒÈκβ»ÊǼìË÷»ò³¤ÆÚÐÔ²Ù×÷µÄ´úÂë¡£
ÎÒÒÔΪƾ֤ RFC ÖеÄÐÎòʹÓà PUT ºÍ POST µÄÀûÒæÁè¼ÝÁËÈËÃÇÒÔ²»ÇÐºÏ RFC µÄ·½·¨Ê¹ÓÃËüÃǵÄÈκÎÔµ¹ÊÔÓÉ¡£
ÒÔÉϾÍÊÇREST ºÍ HTTP ÓïÒåµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡