Andthen I'm gonnashowyouexactlyhowtoconnectto a realdatabaserunningonDocker.
Sowentoutfurtherado.
Let's getstarted.
Allright, amigos.
Welcometoamigoscodeinthisvideoon a teacherExactlyhowtousespringput 2.0, SpringbowisbyfaroneofthebestframeworksforbuildingapplicationsintheJavaworld.
Soyougonnabuildanenterpriseapplication?
Youcanusespringbookorifyouwanttoprototypeonideaandseewhetheronand, youknow, itworksthatyoucanusespringbutbecauseitmakesitveryeasytoebuildapplicationsandbasicallyspringboardprovidesyou a platformforbuildingbackandapplicationsandonLeethatyoucanalsobuildWebapplicationsaswell.
Sointhisvideo, let's goaheadandbuildthisawesome A p I whereanyclientscansubmit a request.
Sotheserequestscouldbe a getrequestposttheleadandput.
Andthenwe'regonnatakethatrequestandwithintheAP, I layerorcontrollayer.
Afteryouperformsomebusinesslogic, thenyoumightdecidetouse a databasetoperformsomevariouscurrentoperationsandbasicallysothislayerrightheresothislayerisresponsibletoconnecttowanydatabaseoffyourchoiceSothecoatingaboutspringbookonDependsinjectionisthatyoucanstartwithanimplementation.
Andthenifyouwanttoswitchto a differentdatabase, yousimplyhavetochangeonelineofcode, and I'm gonnashowyouexactlyhowtodothatwiththependencyinjection.
SoafteryousubmittheactualrequestfromanyclientsoftheclientscouldbeanIOSapp, a react, orevenan 100 up.
Therequestwillgothroughalltheselayersandthengobackwith a responsetotheclient.
Sotheresponsecouldactuallybe, forexample, a Jasonpayloadoranimageor a statuscodeoff 205 100 soonandsoforth.
Soifyouknow, landthebestframeworkforbuildingjobapplications, sticktothisvideobecausethereis a lottocover.
And I'm prettysurethatafterthisvideoyouwillbeverycomfortablebuildingbackandapplicationswithSprinkles.
Solet's goaheadandgetstarted.
Sotheveryfirstthingthat I wantyoutodoistonavigatetothiswebsitestartedspring.
Though I oweandbasicallythiswebsite, youcanseethatit's itstitle.
Wouldthatstopandthenopenupthisdamn a folderandthenopenuptheactualpalmthoughtXMLSo I'm gonnaopenthatupandthenopeniceproject, so I'm gonnaallowandjustkeep a secondmaven A's resolveindeed.
ButtheonesthatwereallycareaboutarethepalmdotxmlSoif I openitupandseethatthisiswhatyousawwhenconfiguringtheprojectwithspringinitializeersoyousawthegroup I D artifact, um, nameandthenversion.
Sospringboardstart a web, andbydefaultspringboardsshipswithspringballstartedtestforwritingunitonintegrationtests.
Sogoaheadandclosethisbecausewedon't needitandopenupthesourcefoldersoopenup, sourceandthenMaine, andyoucanseethatyouhave a job, andtheninsideyoushouldhave a, um, classcalleddemoapplication.
Andthecoatingaboutpropertiesisthatyoucandefinepropertiesfordifferentenvironments, soyoucouldhave a demoenvironment, a testenvironmentand a productionenvironmentwithdifferentvaluesfortheactualproperties.
Soit's prettycool.
So I'm gonnacounseloutofthat.
Andnowthatyouknowtheactualstructureandmakesurethatyoucanstarttheapplication, you'vesuccessfullymanagedtostart a Tomcatserverwithspringbook.
Sonowlet's goaheadandimplementour A p I withallofthoseresourcescontrollersService's andfinding a databaseaswell.
Soif I gobacktothisdiagram, sorememberthat I saidwehadtheAPlayercontrollerorcontrolthatlayerandthenwehave a servicelaterforbusinesslogicandthendatabasefordataaccess.
Solet's goaheadandstartthingsrighthere.
Sowe'regonnastartrighthere, andinfact, letmeput a dotrightheresoyouknowwhatwe'redoing.
Soif I removethat, basically, let's putjust a dotsoweknowwhereweare, um, withthings.
Sorighthereandthenif I collapsedThatSobasically, we'regoingtoimplementthissectionfirstandthenmoveourwayup.
Oh, goaheadandcreate a classandthensimplysaypersonandthen d A.
O Soinsteadof a classchangedthistoaninterface, andthisistheactualinterfacewherewe'regoingtodefinetheoperationsallowed, ortheactualcontractforanyonethatwishestoimplementthisinterfaceandthequiltingaboutitisthatwecanthenusethepenanceinjectiontoswitchbetweenimplementationswithjustonelineofcode.
Solet's goaheadanddefineourveryfirstmethod.
Soforourdatabase, wewanttoeinsert a personintotheactualdatabase, sowewillsimplyreturnaninteger.
Andbasicallywe'regonnamark a databaseusinglike, a list.
Sobasicallygetsisforretrievingdatafromyourserverpostsistoadding a resourcetoyourserver, putmodifyingandthentheleadfortheleading a resourceresourcefromyourseven.
Sotootillspringthatthismethodwillbeusedas a postrequest.
So, obviously, um, wewanttoadd a personfrommyclient, right?
Sogoaheadanddownloadpostman.
Sopostmanallowsyoutoprettymuchjustserveas a clientbecausewedon't have, forexample, a reactapplicationoranandroidapporyouknow, youwedon't haveanyanyanyclient, right?
Sowecanuse a postmanasourclient, right?
Sogoaheadanddownload, postman.
It's anamazingclient.
Andwhatweneedtodoisactuallysend a persontoourserver.
OneiswehavetodefinethesepropertiesinourPoJosothatwhenwesendthisJasonSpringknowstotakethispropertyandthenconvertthatinto a Javaclass.
Right?
Soinorderforustodothat, whatweneedtodoisopenuppersonsoinsideofthemodelpackage, open a person, andthenyouseerighthereintheconstructor, let's goaheadanddefinesomeproperties.
SoourJasonpropertyandthiswillbethe I d andthenrighthere.
Soif I putthisonlineatandthenJasonpropertyandthiswillbetheactualname, right?
Soitcouldalsodefinewhatproperties.
Butas I said, let's keepthissimplesothatyouunderstandexactlywhat's goingon.
Soopen a postmanandthenlet's actuallytryandsendtheactualsamerequest.
Sowe'regoingtosendJamesBond.
I canseethatworked.
Andnowlet's goaheadandtryandperform a getrequesttoourselvesSotheexactsameeurosend, andtherewegosoyoucouldseethatwehaveanarraywithonepersonthatcomesfromourdatabase.
AndthenpassedtheideaandthenopenupthecontrollerSopersoncontrollerandthiswillbe a getsobasicallywewanttopublic.
Sothiswillbe a personandthengetandthenpersonby i d Andthenwe'regoingtopassthe I d and I'm gonnashowyouExactlyhowdowegetthat?
So I d Andthenwhatwewanttodoissimplesafereturn, andthenpersonservicedotgetpersonby i d lowercase I d Andthendon't orelse.
Andthenno.
Sorighthereyoucouldactuallyhave a custommessageor a customexceptionor a customcodethrowntotheactualclient.
So, forexample, a 44 with a messagesayingyouwerenotfound, butforthisvideo, let's goaheadandkeepitsimpleandsimplyreturnNo.
Right.
Sonowwhatwewanttodoisif I open a postmanisyouseethis I d.
Righthere.
Right.
So I wanttobeabletocopythe I d andthensimplysayFortslushandthenpassthe I d.
Andthenwhen I sendtherequest, I shouldgetthatpersonback.
Right?
Sothewaythatwegetthe i d fromthepathisbysimplyusinganannotationandtheannotationiscalled.
Sothisoneisatandthenpathandthenvariableandthepathvariablethatwewantisan I d.
So I d justlikethatandbasically, thisis a typerighthere.
Sonowlet's goaheadandtofindtheactualmopping.
SothiswillbeactandthengetandthenmypinAndnowbecausewewanttohavetheactual i d inthepathsoinside, simplysaypathandthenwhatyouwantisdoublequotesandthencurlybracketsandthenpass I d.
Sobasically, thiswillallowyoutohavefortslashandthensome i d.
Andthenrighthereyou'resimplygrabbingthe i d andthenturningthatinto a u itright.
Solet's goaheadandtestthis.
Soif I prettymuchjustrestarttheactualserver, therewego.
I'vebeenrunning.
Soif I open a postmannowweneedtoissuesomerequestssoyoucanseethatyouknow, everytimethatwewillstarttheserver, weloseeverythingfromourlist.
Sowhatwe'regoingtodo, it's simplysayreturnandthengetoractuallyselectpersonby i d.
We'regoingtopassthe I d.
Andthenwhatwewanttodoissaydotandthenmap.
Sorighthereweget a person.
Let's callit p Sopee.
Andas I said, ifyouwanttolearnabout, youknow, joblessdreams, maps, operationsfiltering, goaheadandcheckmyYouTubevideowhere I teachallofthis.
Sonowlet's goaheadandtryandgettheactualindexoffthispersonsointandthenindexoffandthenpersontodeleteequalstwoandthen d B don't andanindexoffandthenpassedtheactualperson.
Right.
Sonowwehaveanindex.
So I'm gonnasay, iftheactualindextodeleteand I'm missing a needrightthere.
Andthenwhatweneedtodoissimplysailreturnperson d 00 don't AndanupdateAndthen I d rightsoyoucanseethat I don't havemuchlogicgoingonhere.
Andthisisbecause, youknow, I'm justteachingyouexactlyhowtouseaminospringbuttoetocreate a B I essentially, youknow, insideoftheserviceyouhavewhateverlogicyourapplicationrequires, right?
Andthenrighthere, let's goaheadandperformingPuttothis, um, enpointeandsimplypassedonJones, youitor I d AndthenintheactualbodylessalsochangedthistoAnnaandthenMontanajustlikethat.
Andthenifwesentsowego 200 nowthisshouldwork.
Sogoaheadandsend.
Andthat s youcanseethatthisnowworks.
Soyouseethatweprettymuchjustmanagedtoimplementallofthemethodsthefindinour a p I.