Implement REST APIs for CRUD Operations - Part 1
Let’s learn how to design and implement REST APIs for the CRUD operations like create and read.
We'll cover the following
REST APIs for Todo
The majority of operations on objects are limited to creating, reading, updating, and deleting them. So, let’s take the TodoController
class and update the body of the methods like create
and read
. Then, we’ll implement the logic in the TodoService
for CRUD operations on the Todo
object. Last, we can verify the REST APIs for such operations using the cURL commands.
Create Todo
Let’s look at Todo
REST API POST request flow in the diagram below:
Let’s update the create
method in the TodoController
class to accept the request parameters and bind them to the Todo
object by using the
package io.educative.controllers;import io.educative.domains.Todo;import io.educative.services.TodoService;import io.educative.services.TodoTypeService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpStatus;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.*;@RestController@RequestMapping("/api/todo")public class TodoController {private TodoService todoService;private TodoTypeService todoTypeService;@Autowiredpublic TodoController(TodoService todoService, TodoTypeService todoTypeService) {this.todoService = todoService;this.todoTypeService = todoTypeService;}@PostMappingpublic Todo create(@RequestBody Todo todo) {return todoService.create(todo);}@GetMapping("/{id}")public Todo read(@PathVariable("id") long id) {return todoService.findById(id);}@PutMappingpublic Todo update(@RequestBody Todo todo) {return todoService.update(todo);}@DeleteMapping("/{id}")public ResponseEntity delete(@PathVariable("id") long id) {try {todoService.deleteById(id);return new ResponseEntity(HttpStatus.OK);} catch (Exception e) {return new ResponseEntity(HttpStatus.BAD_REQUEST);}}}
Next, we implement the create
method in the TodoService
class to save the Todo
object.
For now, we can use a Map
as in-memory storage to hold the Todo
objects. We’ll substitute the Map
with the database in upcoming lessons.
PK A META-INF/ PK A m±>=@ ? META-INF/MANIFEST.MFóMÌËLK-.Ñ K-*ÎÌϳR0Ô3àåòÌ-ÈIÍMÍ+I, êdä¤Z)¸%¦ä¤*%¤ñrñr PK A org/ PK A org/gradle/ PK A org/gradle/wrapper/ PK A %Ó¦¹ / org/gradle/wrapper/BootstrapMainStarter$1.classRËnÓ@=Ó¸u1¦ôEy@_IikÁ6U@.,RuØLì!ÊGã ü6 ±àø(ı ]t1ssÎÕ¹÷ÎÏ_ß x½ 󸳻îá¾M|4i£ÝsZ«}.à穸k£Þ=eÏd/c¦çÌÎ¥Õe<Mzn ð1Êg²(ÃÇqnûQßÊ4SÑ'+G#e£yî ÇàTjÓuÒ:e·t(@&9V|!?ÊHçÑK©Î$ʤéG]gµéwÚï(3ÕV`é_(9¬TþGºùØ&ªD l\&ä¨d±ëdyAÖ©r<õñ0Ä#l ð±(°>ié0c=<ÚO¦õ±b»í+7.Pÿ£ômïB%ÀÚßM Ñ-°wźlá6i<øZ2³93Ööìèê³#ðÒ± w¹Úºl hÂãâòø¹æx8F×èE´v~ÿÄê9à½P%pw8Ð.ÑR3êSò ѵ²ÜAcî+jgèQEoN SzéÝB£z÷±ªÄjÅ\Ã:ÛØÀ zQ^ÅÁoPK A i,« $ - org/gradle/wrapper/BootstrapMainStarter.classVÛSWÿsÙ°,1*±BI /µV°V(Ð-m´Ú.É!¬nvéf£Ðjï½<÷·þ ö%Ø2µÓv¦ãô;»IIb¼ÀÌÙ³ßõ÷]7ÿ>ýý1£øIF 2^à C0%cïÊðA0#aVFd\Æ{2æǼd\ÁUq¼BNP® p]ÂR2"¸!c7%|Ø^|$AaQØÊ£ .aIÆ~ ¢eeÊ4¹1´r§tSwN3øÉ+þUàÛUÝä³Ò"·çµE(aÕÊkÆÍÖÅ{èwu²T-».ÚZÁà黶¶²Âíô9ËrʽÌhºu4Ûáö8C ,®ëê-í64³Î:¶nÇ=n¥/èwñhv<ìl#Ì {.'aénVfèð¸ÓMèóéðUÇ [µ´'bÔÓ0¹^SXB¹D¨]Ãß.I¸üî,[XÍwÒ$w7'nm¥¼H«ÙSçI¾r¿=£¸btr6±ç+ne ·¨:KºYPµ_æ¶_¢%uÉP-q iûõgôÅóºMÐ-{\fçIjWË0 ` NãÊÂV8sÓÑKüØ«bâ¦åÄ ÊÃãÎ2_t+¯cOëf\Ðɧ¾XâCå¡CÏK·Û ò³ç9%¥ 9 ,¬HøX Ê©£ ; îc «Ö|OÜÃ}Ê.µwÊë¦TkÊ)¢à3|N*ZRÛRU_àK_ákê¤zÒ3U梾 ¾Á`\4Tä[ßáïñ¦ìlàð#ÃÐ+*0ô>¯iò¶X åìiFZ$K]Òh²O&ÕÆñõêVoÑæÞÕFW¬c-ÌMßD²qh§Æë:UTT ñÊÔ;#RäNvìðRózH$['tk=ÊeÍæ&Ŭo²º§çh8ò6¿lsܯØÂPý=ÒäÖ£ ðeîdÚ,±Xâî:º×¶Ø`»Ô¶Ùl"´zñϼ¤NòEë/¨w¬Û¼¥-j=𶨨¨y1/[Ö\ûf òUÚe÷v¡S/7,¶xBY|·ÍZI,<ñJ¶ìG x âÏ&¯Ó[á °_é² Ct]b :O IÓa£$EÊì$úî>¶ÙCÿ w¾\Øïÿo4ûÁ Hn"#RGÎw¨ ¹Nb(Utm {ÌÿÛ£þ*z¢þð*Âch ¼¤#9_xW¶ÝcÁhyGè}ÏÕ¨ÿ/ô®CúCAö» Sè¤s/AÝô¡~8#E¯Rü7q%½BÑß# ܤ@-8q;Dv{Kã0À*ÐÍG¶ÎÑ¢cäM%Ê8N¼äãM ½UKÇ;I¼1¢D°í ú%ÄâDvN¹Þ¦Dôí©Õâg¢ùèy|±Üpxïö©#UôcýëèÙÄþÜâUý]£ý¿@õ}Ⱥ¥iP8T©(Õj@§ ö1*BÜ ºqg)<rç ¦ <öÇ8Édܾ8OédâïwS ÿ PK A hQþ}¢ Ò # org/gradle/wrapper/Download$1.class}M Â0 ßh5µ*v/×½?àB\xØØT{7ÀC)âÖæ ¾÷^ïÇÀCÆ0 $g{sÜZÆ[ÛmE¾,Å]&;i룲¾Ú!!̬S\9kÉ'ªJ:þË-VÑÁé6Zx/=!m«¸FñÓ¥YMþiHç tðnX¡~~Ð8\j4þ PK A y´[À4 Ø A org/gradle/wrapper/Download$DefaultDownloadProgressListener.class¥UßSUþn6 eRH(Z@Û%U+ ik[ J%D«Î%¹ Û.»¸Ù@ÿ>øÔ¾èÓ_:#Bu¦}Óþ#þzÑÑqF=÷~t 8ɹ7÷Üs¾ï»çìÝ'ýÀ+øÈDo4¢ÃÒH5ÉmÀ9çñf¸ W.¸Ñ&aÜ@ÎÀ[&¢x;raêc" i.4pÅÀCÄñ*á3t<¿©ø¼ìÌªÏ )(g¡¡,Qá`8µ×Æ1oÕu<^¾ê{_T«»W ^öÅíÕªÛ{_nÀÀòÖvíà<ÃåÔ²o!<êksÁvÅÚÒðgøC+-¯ÄYîÛò}1,ÚUî1q×`¿ÌVÞ¥qÔáÕª sñ:þÙèh庯ð V]änEZS | g\d®Mç³RÁËe!ö´!Zò\J[ éd'Ìä2cJ·&ùr]qRru·JÁ+¢èå8 ÔSXm¥ïçÙÉàMxÛËä§ÆoÄr`{.±h§-ÕêgÂT^*wSàÜ٢ߡ¸µd½ÅälÉ2ºgPZèÂUm8l¡ ÓÂ)«AïXFÑ ´è¹yÕÀ k50'=ïÒ±îAáZuèè3Ó57°Ä¶S"ÎxÏÂû¸ná\7ð!CöTDïàM-Ü%:þçho-%+Ô±ïÓBÕfuN×I¡Q{ùEñqM¸%íÛ帨vËÆÈ>E¯ø¶[¡Æ[áNMLÝ Ê§ò»77PPb¯dÄ15º/Nûnj3¾·ªRZlÇ1ÉeShKü¶ì¡|^ö¶dSÙ:83ôÐm ñ Ù1týµÈ¦¡1$ûÆ0ù»pì1ú·@ëô:Xº ¡ôÀ´´¶ðÕM¶ :Ùï)ö4âGÄð!ýLx G/«ë6¡pIäÍ©7ëx#B_`¤§ñî§8þÚw0Ó=máuèdïÞCC-¯`ÌI÷·Ë¡Ñd¥)VÇ`ý .û_ ë7R÷;!ýüaü¥Ø¥3¡m³ÁKH)v#è#oHÍúÕ¾ëÐñVZ¦ç«NùIr.n¦»{×Ñ7Å!=©ßÃIbNêá¨äÔ7`íøh a.Ôéï£yDÿÑù}±Çø´ê2^@Ù#G£æe!±YY¦cJÊ"#òª#KïµÓxY&·-/§D15¢4%ªÚÐ%V颢®kTÉèD¼¬õÞé¾ã%Ð|ë Ú@ËÛ¥ÈlÌܧÔgêh ¼¦Ð¹|/ª7/]>(Bó_PK A Û¡ ~ 4 org/gradle/wrapper/Download$ProxyAuthenticator.classUÝsSEÿíMèMn74ÄJ©X©1IÛ$Eü E ¤ `úa¿ ´ÛôâõÞxscé?ä3:¢Ì8>éÌ8:>øWøàÃÙÛ´¤;éî9»¿söß9»÷Ñó~p%G1¥á´>¼«Ô3Q¼÷ôqÖÀÆu3U(>à¼/tã"&t\ Á¤ËzpÅ@+o踪ãC¿nUy¢ëseOlÛðD¥"½Ü¤»áØ®(ðåXþyÁTGdz!\pK¡§h9rºöåôÄM+¢k {IxÒaíÍzî½Í¿.ß2 ïzüªãH¯`jU&ßéèä^wYú³d¾áz¥¦=ËuN¦ÒÅ»âks¤ÛDbåwg[%ØØÂ)çæ}ÏrÊÈwM×føŪ¤øcó¾0¿ W2\èÌ_ÇGzµ{y528 Í`Ì»5ÏW,Åtlg+«2àx Ç9¡_IE)ÌèåøsóXбȱë:np,ã&C_{æj]Ç Vé°ìnê+Ã[·q!ÞnÈp8ÛÆ+(ªÁ!°¦¢59N"ÉýýÀpt÷¶L2Rú8uÓüª&«d´°YQ¤¶é©æ¦j9$Ù¤â$_ÇphvnæÆ2éõÛ%Q½8W¤¥æ¨h P¼YgèVe·{wðm~¼KTa²ZMæ©A³¼Ûîk¾eçè~¨F!r3Ú{ôÞh§îïî4U¬Å)U"âάݦßâ°±Døna]x'6éaJ¥W Ôôû³RH/aÞá>z«Y<®nI!hêRQ'¿NÚÒ4ÌÐÐ2[}K»<Æ4ò@6poÑVÝðð/iak:ÂßáÐ7°HêªC/f¾ö .oÁª£;Ái¨#ö#kø=SÿblX¼6=òºG¦"±<<RÇ+[è½OÁ¾z?HH{¾(Àïâú<¥oÐ3ÅXŸ¿`âoTðOL^E/Þ&«¡oáÙ#E"¤ÖH5MÿG =©£gû÷xÄáPI2)¾2C[`ûó7¸h8UÒ0UÒ²AreoByQåÆÈN£pé/¡ðú0}±ÿPK A pOÌ) ¨&