Java OUT, Kotlin In: How to Migrate Your Spring Boot Projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we're going to convert a Java spring boot project into a cotlin spring boot project automagically and we're going to learn some cotlin along the way for those that don't know cotlin is a programming language and it can run on the jvm much like Java but it can do a whole bunch of things that Java can not so we need a spring boot project that's been written in Java so we're going to use the one that we created as a part of the spring boot for beginner's course the rest API for dealing with books and their authors now you don't need to have taken that course in order to use the code for our tutorial today but if you did want to check the course out there's a link in the description below it's up here for free on YouTube to make things easier I've taken that code and I've put it into its own GitHub repository so if we take a look over here we've got this URL and in this URL we've got the uh zero Java books we're going to be adding more to this as we go on but inside of here is the spring boot Java project that we're going to convert into a cotlin project so what we're going to do is grab the URL up here and we're going to use this to clone this repository down onto our local machine depending on which operating system you're on and which tool you choose to use this might be a little bit different but I'm just going to use a straightforward terminal as I'm on Mac here and I'm going to go get clone and then the URL and that's going to clone it down to my local machine great easy as that and now we're going to open this up in intellig using intellig for this tutorial is very important because the same company that's behind intellig jet brains have the same masterminds that behind the cotlin programming language so as you can imagine intell has unpacked paralleled support for cotlin now there's two versions of intell there is the free version and the paid for version we're only going to need the free version for today if you have the pay for version awesome you can use that too so with the project open in intellig we're just going to make sure that it works before we try and convert it over to cotlin so we'll open up a terminal down here and we're going to use the maven wrapper that's being bundled along with the project and we're going to go Maven clean [Music] install so there we go build success looking good and you may have noticed that a whole bunch of tests run as a part of that build and we're going to be using those tests in a moment when we convert it over to cotland to make sure our project Works absolutely fine once it's converted but before we start we're going to need to know a little bit about this project and I think a minute is going to be enough to break this down so this project is a crud rest API and that stands for create read update and delete which are operations that we can apply to certain resources in this case the resource are authors and the books that they write an author can write many books but in this system a book can only have one author this project is split into three different layers with the upper layers relying on the ones below it at the lowest level we have the persistence layer which is concerned with reading and writing from our persistence which in this case is a postest SQL database in the middle layer we have our business logic or service layer and that's where all of our rules and our logic is implemented and at the upper layer we have our presentation layer where all of our information all of our data is exposed to our users in this case via a rest API a rest API with create read update and delete operations for both books and authors this project is using spring boot version 3.2.2 and is currently using jdk7 but should work exactly the same on jdk 21 and it's currently written in Java we're going to convert it to cotlin so Vanilla Java projects usually have a whole bunch of boilerplate code and it's called boilerplate code because it can be automatically generated there's nothing novel about it I don't really know where the boiler comes in but besides the point so all of this code can be generated so in the Java version of this project this one we're looking at right now we're using project lombok in order to generate a lot of this boiler plate code for us however lombok doesn't play nice with cotlin so we need to remove lombok D lombok the project before we can convert it into so we're going to go to the classes that have lombok in them which in this case are the domain objects over here and we're going to delomo the project so we can right click on the class here we're going to go to refactor and we're going to go down to D lombok and all lombok annotations and we'll notice all the lombok annotations at the top here will be replaced with Vanilla Java so we can see the class is look a little bit different this green line obviously highlighting what's changed and we can see here we've got the Constructor we've got a builder all of the lombo have been removed and replaced with Vanilla Java now this approach seems to have worked absolutely fine for every class that I've tried it on so I'm going to quickly apply it to all of the classes that have lombok in right now and I'll commit this to the repository as well so you don't have to do the same thing and you can just skip straight to the cotlin and that's all done now we're going to add cotlin to our project and this is how you do it we'll go up to the toolbar up here we're going to select tools we're going to go cotlin and we're going to select configure cotlin in Project we'll select all modules and uh we'll go with with a cotland compiler runtime of 1922 that seems to be the latest one here big version two is on the horizon but we'll use this one for the minute and we're going to click on okay okay so that looks like it's worked but what's changed well we can see the pom file has been brought up here uh so let's take a look through the pom file we mentioned earlier these little green bars on the left hand side highlight differences and we can see that the cotland version property has been added here version 1922 the one we selected from the modal we scroll down a little bit more we should see some more changes okay so we have a dependency here of the cotland standard library then a cotland version proper to use we also have a cotland test dependency looking good and if we scroll down here we have a plugin which is the cotland maven plugin which is then used for using cotlin with with Maven H it's got a compile configuration in there looking good so it looks to me like cotlin is configured for our project now I happen to know this isn't everything that we need but it's a great start so we'll add a little bit more to this as we go on but now for a really big gotcha when you do this you absolutely need to remember to refresh your Maven changes otherwise intell can get a little bit confused and uh say you haven't got any coton in your project and recommend that you add coton to your project and add multiple versions of the same dependency to your pom file but obviously you only need one set of these dependencies in your pom file so doing this will then refresh everything so intellig knows that cotlin exists in that pom Fon so it looks like cotlin is configured in our project now let's just run the Bild quickly and make sure that it hasn't broken anything Bill success looks like it's working absolutely fine but we still have Java files we go over here we can still see that our file ends in Java and we're looking at Java code but we have something new now so if we right click on this file and we can go down here we've got this option to convert the Java file to a cotlin file and we also have this uh pretty complicated key pattern to do that for us you might have a different one if you have a different operating system or if you have different key bindings but this is one that I have at the moment so we could rightclick on the file and and click this option and convert this one file into a cin file but there is another option as well if you go up here to the Java directory if you right click it you can see that you don't have the option to convert everything into Colin but if you use the key combination you can now have an option here do we want it to try and correct some of the issues that might have occurred yeah why not so we can see now that the Java files are now cotlin files now let's do the same down here to our test directory as well okay and it looks like our test directory now contains cotlin files instead of java files we've got this KT extension instead of java so is it really as easy as that let's see no unfortunately not the converter has done a great job but it can only do do so much we currently have a broken build but that is good news because this is now an opportunity for us to fix it and learn some cotlin so let's start at our application's entry point so we'll go over here to books API application and this is the thing that's annotated with that spring boot application annotation you can see here though that things are a little bit different we have ourselves an object of books API application we have this jvm static annotation and we have a fun function fun function as opposed to a main methods but we do still have this spring boot application run passing in our books API application class and we've got some ARS there as well so let's start with the object here so an object is like a class however you can only have one of that object it's a Singleton whereas you can instantiate as many instances of a class as you like now the equivalent in Java might be a static class for example you can't construct it because it's static you can think of that as how we would use an object inside of column now inside of that object we have this main function of course is the equivalent to a to a method of course it's much more fun and this is the equivalent to our main method here and you can see we've got spring boot application run and then we pass in that class we also have this jvm static annotation here now this is optional really however it does make it easier to call whatever it's annotating from J Java you can use cotlin and Java interopa cotlin can call Java Java can call cotlin however it's usually easier to just use one or the other they're interoperable however spring boot wants things done in a particular way for this entry point so we're going to make some changes first things first we don't want an object we're going to have a class and we're going to remove these braces here let's get rid of that jvm static and I think we're going to bring that indent in and there we go and this might look pretty weird so we have here a class of books API application and we're annotating it with the spring boot application however we've got no body because there's nothing that would go in that body so cotlin allows us to emit it we don't need any braces we just have that that declaration there now we also have the main function which is existing at the same level as our book's API application class it's not it's not contained within it's it's at that same level and uh that's pretty novel that's pretty different and there we go that is how spring boot wants things so let's move on to our presentation layer taking a look at the author controller first I think so if we take a look here we still have our rest controller annotating our class author controller where we can see that the Constructor is moved from being inside of the class to being at the same line as the class declaration here so we can see here we've got our dependencies our author service and our author mapper that is exactly L how spring would inject them they're declared right here but they're also declared with this private vowel keywords so essentially this in cotlin is called a primary Constructor and it's the equivalent of declaring a Constructor but also the instant variables to which the Constructor would uh would assign them to if that makes sense so essentially what we got here is a Constructor but we also have two instant variables or properties in cotlin of the AU service and the author mapper uh they are private in scope to the class and they are vowels which is equivalent of using the final keyword in in Java which means that once they've been assigned they cannot be reassigned so if we take a look now at the end points instead of our methods we have our fun functions and we have here the create author functions still with the post mapping and if we take a look through it looks fairly familiar to Java I mean the um the variables and the types are inverted but we can kind of figure out what's happening here except for a couple of things what on Earth is this question mark and what's with all the drama what is this double exclamation mark doing here which I think is called bang bang right so bang bang what does it do well you see in Java non-primitive types can be null right and you don't really know if they're null or not until you try and call something on them and then boom you get yourself a null pointer exception that's very costly in the grand scheme of things so cotlin fixed it and the way that it fixed it is by having nullable types so you can see here the author dto has a little question mark next to it and that means that author could contain or or reference an author dto object or it could be null if that question mark wasn't there then author would never ever be null and here's here's the funit cotlin the cotlin compiler actually for forces you to deal with cases where a variable could be null of course there are ways around this and this is one of them so if you did want to deal with things exactly how you would do in Java so for example by pass the cotlin compiler trying to help you out you would use this bang bang and what this means is you've got yourself a nullable author entity put bang bang after it and you have yourself an author entity one that's not null one that the cotton compiler won't force you to deal with of course that's pretty dangerous because if it does end up being null and you try and call something on it you're going to get yourself a null pointer exception and uh yeah there's going to be very little value in you having these nullable types so as you can imagine this bang bang operator is pretty frowned upon when it comes to cotlin the converter has used it for us and we will use it but we're going to follow the Mantra of getting it working first and then making it work well after afterwards but just know in the production environment that you may be coding in you don't really want to use this bang bang There are better Alternatives luckily there's a bit of a converter artifact on this one because for the Post mapping for the create author we're always going to expect an author so if we remove that question mark there we don't actually need to use these bang bangs at all there we go so we are making excellent progress if I do say so myself but we are only scratching the surface of Goin if this absolute Beast of a programming language is peing your interest and you want to use it to build real world projects then you're going to love this I'm currently working on a full course with the objective of teaching you how to write spring boot projects using the full power of cotlin and I am really excited about this one as usual I'm going to be releasing a ton of free content here on YouTube but this time I've listened to your feedback and produced a ton more premium content which Builds on the free content with the objective of building a production ready secured and monitored real world Spring boot application written in cotlin perfect if you want to bring cotlin in at work if you're trying to get a job that uses spring Boot and cotlin or if you're building your own apps with one of the most popular jvm Frameworks and this absolute Powerhouse of a programming language to learn when this course drops and get an exclusive discount then join the mailing list by checking out the link in the description below okay back to the tutorial so that's create author compiling there we've got list authors which is compiling no errors here we can work out what's happening we've got these authors here we're calling map on there okay this is very similar to the Java get author again seems working absolutely fine okay it looks like everything is compiling on the author controller so let's shoot over to the book controller here okay we have ourselves a compilation error so the reason for this compilation error is pretty interesting so we can see here that we are returning an if statement to begin with so that's something that's new with cotlin an if statement is actually an expression you can return an if statement rather than having the return inside of the if statement of course you can also include the return inside of the if statement but this is something which is very Colin so why is this got a red squiggle under it and uh we can see here we got the response entity with this any type so the any type is Cottin's answer to the top level object type in Java we don't know which type this is it could be any type so why why is this here in the first place well I don't really know to be honest the converters put this in but we should be able to work out what the type is because well we've we've got this saved updated book dto we we've got the book dto type there and the book dto type there so cotlin should be able to infer it so if we remove the type here we can see that it compiles absolutely fine doing a quick scan of this one we can see that the converter couldn't really tell which were nullable types and which weren't obviously in Java any object that's not primitive could be null so that's what the converter's gone with we won't spend our time converting those to non-nullable types unless we absolutely have to but it's worth noting so that seem to be our presentation layer working absolutely fine let's take a look at our service layer now so we'll start with the author service interface here we can see it's just uh normal interface we can work out what's going on it's a little bit different from java but it seems to be pretty similar now the fun thing about interfaces in cotlin is they can actually contain implementations as well this one doesn't so let's go check out the implementation for it we have our author service Ino here we have our primary Constructor passing in its dependency of the author repository and uh yeah we've got some compilation errors so this override keyword is underlined and the override keyword is there you need that in cin to say that you are overriding something which is in a super class or in this case the interface so we can see here that rather than the implements keyword we are basically using this colon to say that our class of author service Ino is is of class author service extends it it implements it which kind of makes sense if you think about it so if we go over here we can see that save has an author entity which is a nullable type but we're getting a compilation error because this one doesn't have a nullable type now I'm going to say that if you're calling the save function you want to call it with a non-nullable type because can't save a null right so we're going to change the interface here save it and go back and we can see it now compiles absolutely fine because we're overriding something with the correct signature with the correct types so you can see that cotlin takes its nullable types very seriously now for find all and this is failing for a very interesting reason so we can see here we're calling stream support which if we check out where it's imported from is imported from java so we're calling down to the Java libraries as opposed to the cotlin ones obviously it's cotlin but it's doing the same thing as it would do in Java and we're getting a compilation error because we're requiring our function is returning a list of author entity but it's finding a mutable list of author entity with aable type so what's that all about well in cotlin by default collections are read only whereas in Java of course they are mutable you can you can change elements in them you can do what you like now notice I said readon and not immutable and there's a whole bunch of nuance around that you can actually change the readon collections in cotlin but just don't it gets really really complicated um however if they're read only then they become a lot easier to reason about this particular type conflict is because we are getting a mutable list Java list when we want a cotlin list so you mentioned earlier if you mix cotlin in Java you can totally do that but things can get a little bit complicated and it's usually simpler just to go of one or the other so we're going to replace this mix of cotlin and Java with just straightforward cotlin so what we'll do is remove everything except for the core to find all here now we we know that we want to return a list of author entities so we've got find all which is going to return ourselves an iterable so we're going to call the coton function here of two list and that's going to return us a list of author entities but that is currently nullable so the it's list of author entity if we were to do this then it would uh it would work but that's not what we want to return that's not what we're overriding so we're going to onto list call the function filter not null and that looks a lot simpler and that's all we need to do in order to implement that using cotlin it's basically going to take that nullable type filter out all of the elements where it is in fact null and only return the ones that are not null thereby changing the type that's returned from a nullable type to a non-nullable type okay on we go find one seems to be working absolutely fine it does have a Java optional in there though where potentially it might be cleaner with a nullable type we may come back to that in a moment it is compiling though so that is our main goal partial update is not compiling why is that well we can see here that it's calling set ID on author entity so set ID of course looks like a Java Setter here cotlin does have the concept of a Setter but by convention they usually share the same name as the property or instance variable that they are uh trying to set for so in this case rather than set ID we would just call ID like this and we do have a red squiggle why is that AR okay because our author entity is a nullable type so this is the C compiler making sure that we handle the cases where it could be null so one way of doing it is by using the save call Operator so this basically says set ID on author entity if it's not null however this partial update is always going to receive an author entity we don't want to be passing an anull to this function so we're going to go up to the interface here we're going to remove the question mark from that making sure that is a non-nullable type and in doing so we can see that move it there also we don't need that save call Operator because it's always going to be nonnull so let's get down to this then so we're calling find by ID and then we're going map on it so find by ID that is returning a Java optional so we mentioned before we can mix our cotlin and our Java code and that's exactly what we have here but it's usually simpler to go with one or the other so we're going to replace this implementation with the cotlin standard straightforward cotlin implementation we're going to start here by I think just commenting out this whole block just so we can reference it we're going to want to get the existing entity from the database so we're going to say author repository and we're going to call well we were calling find by ID so let's just take a look at that find by ID wants an ID which we can of course provide it and it's going to return for us a Java optional we don't really want to use the Java optional we want a nullable type as we're dealing with cotlin here so instead instead of find by ID we can call find by ID or null and that's going to return for us a cotlin nullable type now this is really interesting because this is a function that you won't find in the normal Java spring data jpa libraries this is actually a function which is provided because it's a cotlin extension so cotlin has this fantastic feature where you can take a pre-existing Library it might be one that you you have written yourself it might be a third party one and you can wrap that library in additional functionality in this case spring data jpa we've wrapped that in an additional function which returns a cotlin nullable type so we now have this uh find by ID or null which going to return a nullable type of our auor entity we're going to call on that question mark because it's nullable we want a safe call and we're going to say dolet now dolet you can think of this we're using it similarly to how we would potentially use the map function on a Java optional it's a scope function so it allows us to provide a function here which is applied to the to the thing that we're referring to which in this case is going to be the author entity that exists and we can actually give it a name here so we're going to say existing author or by default the let function the thing that you're referring to you can actually refer to it by using it it it it refers to it but we're going to Nest a let function in here not something really want to do all the time but in this case it makes sense so we're going to define the name of existing author to refer to the existing author now before we provide an implementation to this I'm going to deal with the case where the author doesn't exist in the database before you can see here with throwing a runtime exception and we might as well just do the same thing so we're going to use the Elvis operator and then throw that runtime exception so the Elvis operator it's called that because it sort of looks like Elvis is hair if you turn your head and use your imagination but in terms of this we're basically saying if the thing on the left of the Elvis operator is null then do the thing on the right so if find ID or null returns null then we're going to throw a runtime exception and you can see here that we're creating a runtime exception there's no new keyword in cotlin so we just call runtime exception and then imper the stuff that we would pass to its Constructor exactly the same as we've done up here now that we've dealt with when the author doesn't exist in the database we're going to deal with when it does author entity the thing that's being passed in if it has a name which is a nullable type string with a question mark after it so we want a save call on it so if it's being provided let's put that question mark in the right place shall we if it's being provided if it's not null then we want to do something with it which in this case we're going to on the existing author reassign the name which is a VAR so we can do that with with whatever is being provided which of course is the name in the author entity which we can refer to by using it we using let up here and we've given it a name we're using let down here as well which by default the thing that it refers to is it so we're going to use it if we didn't provide a name up here it would be ambiguous and we would be forced to provide a name for one of them so we've uh We've jumped ahead and we've done that so we we've got name we're going to do the same for age as well because that's what we're doing up here in this implementation there we go and then it's calling author repository do saave so we'll do same thing here author repository Dove and it's doing it on the existing author we go and let's remove that commented block so something to point out about this one is because we have these braces here the the function that we're passing to let we don't actually need to use a return keyword it's by convention the last thing that is in this function the last line that's going to be returned and we've got here the result of our save function which is going to be our saved authenti so everything compiles absolutely fine here so we now have a working author service Ino let's take a look at the book service interface yep exactly what we would expect pretty much identical to how it was in Java so let's take a look at the implementation of book service okay so if we do a look through here it looks like all of the issues we're facing in the author service import so I'm going to speed through these because you've already seen the solution once so let's get these done okay so that's all done if we take a look at our persistance layer this is still pretty much the same we happen to use spring data jpa which provides the implementations for us so the the interface and everything is working fine for the author and for the book The implementations will be provided as it usually would so let's do a couple of Tidy ups and then we will go on to running running all of our tests and seeing if our application actually works we're going to start with removing those Java optionals because you're here for the cotlin right we don't want Java optionals in here so let's go back to our services start with the author service instead of an optional we are just going to return for ourselves an author entity of nullable type just make sure we got no other optionals in there none at all we're going to do the same for the book Service as well we don't want an optional we want a nullable type let's go back to the author service simple it's now going to flag up that hey we're not returning anable type we're not over writing the right type find by ID find by ID or null that was easy let's go to the book serice simple we not returning an optional anymore find by ID or null looking good but of course we're calling these and uh we were dealing with an optional before so let's find the usages okay so we can see here that it's trying to call do map of course that doesn't exist anymore so we're going to replace that with a call to let there we go and let's do the same over on the book controller there we go so instead of map we don't need that anymore so we're going to replace that there we go and we don't need or else we are going to use the Elvis operator so we no longer have Java optionals let's get rid of everything that was Java so if we look here we still got this Java directory in a cotlin project as you can imagine this wouldn't be Java this would be cotlin so we're going to change that okay that's now cotlin let's do the same for our test down here so the last thing that we need to do before we can run our test and run our project is just get everything configured in our pom file as it should do remember earlier when I said there's going to be some additional stuff that we need to do well there is so let's head over to the pom file now okay so we have our cotlin plugin which is down here but there's some additional plugins that we need to apply to our plug plugin in order to get it working absolutely fine with spring boots in particular the spring data jpa side of things as well so what we'll do then is I'm going to replace the plug-in configuration with one that I have borrowed from the spring initializer so we've got a few things in here which I'm going to point out to you but we're only going to go at a high level so you can see this has compiler plugins for spring no surprise there as we're using spring and then jpa because we're using spring data jpa in addition we've got here the maven all open plugin and the maven no ARG plugin so this is a way of Bridging the Gap between the Java that the spring libraries expect with what cotland would generate all open by default classes in cotlin are essentially final closed you can't subclass them unless you declare them as open so this is basically just making sure they are open because inside of the implementations for spring it expects that and the same for noag so by default fault you wouldn't have a noag Constructor inside of cotlin just just for free but this one will'll put them there for you and this would allow it to work absolutely fine with spring boot one more thing though we are going to need to put in an additional dependency up here so you can see already we've got the Springs down the library we've got cotlin test we are going to need to include the cotlin reflection dependency okay and if we refresh this so the cotlin reflection dependency we're not using reflection explicitly but it does use it under the covers so we want to make sure that it knows how to do that with cotlin so we're going to include that Library as well I do believe our project will build now so we're going to open up a terminal here and where is before we had this failure we're now going to run exactly the same maav and clean install and see what we get okay it looks like all of our main code is compiling and that's fantastic but it now looks like our tests are no longer compiling so let's take a little look at these okay so it looks like it's mostly type stuff in our tests take a look at this one as an example it required a long but it's got itself here an optional long and by the look of things it's mostly the same for the other tests as well so what I'll do then is I'll shoot through and make sure all of the types align and then we'll run this again hopefully with the Test passing looking good let's run this once more so the tests are running so it looks like the compilation has gone absolutely fine but if we take a look here we have ourselves some failures expected NBN but it's getting itself a null what could possibly be going wrong well I think the easiest way to deal with this is actually we just run it in the IDE and then we can get everything linking up so we can see that the controller tests are failing here here's our body it's IDE is null the name is null and the age is null R null that is not at all what we're expecting but it is expected in a way so if we take a look at how we are dealing with our objects inside of the controller inside of our presentation layer you can see that we're using this mapper so we're using this mapper to decouple our presentation layer from our service layer and our persistance layer so in our service layer and our persistance layer we're using entities those jpa entities which basically represent the objects how they're stored in the database it isn't necessarily how we want to present them to the user we might want to do something slightly different in a rest API and if we were to introduce a different presentation layer maybe a graphql API for example we might want to do something completely different there there so it does make sense in a lot of cases for these presentation layers to have their own dto or data transfer objects and to convert from one to the other from our entities to our dto and back again we're using these mappers now if we take a look at the mapper here very straightforward map to map from if we look at the implementation we're using this model mapper which is from a model mapper Library so the model mapper Library uses some pretty clever reflection in order to convert automatically from one class to the other Java Reflection and we're in cotlin so this is a good lesson which is if you are using a library which does some pretty clever Java Reflection you might not expect it to work in exactly the same way in cin model mappa is a prime example and lombok is another great example luckily we can provide a different implementation for our author mappa and our book mappa and everything should continue to work we don't want any nullable types in here if you're passing an in authen you're going to get an author dto and back again so let's make sure everything lines up let's provide an implementation here so we're basically going to construct an author dto passing in the information from our author entity which goes ID name and age as it turns out we're doing the inverse down here in M from but of course we're going from an author dto to an author entity and you can see how some of these liaries can automatically generate this this is fairly boiler plane so that is our author one done let's let's do the same for our book okay so let's remove that nullable type okay so the the book dto actually references the author that wrote it we're going to have to take a look at the Domain objects so I wanted to hold off on looking at these domain objects because uh even though they're technically right they're a little bit involved and it's a direct result of us ding them and then using a converter to go from java to cotlin so even though technically right there's a lot to it so if we see here that the book dto can have an optional author dto in the author variable here so we need to pass in a dependency to our mapper here which we can do via the primary Constructor now we can go book entity now author could be a nullable type so we're going to sayl and then we're going to use our author mapper dot think it was map 2 passing in it good and we will do the same down here here but of course we're going to be constructing a book entity and instead of mapping two we're going to map FL so there's our mappers as this will be quite a nice visual let's now run all of these tests again and see if they pass all of our tests pass so let's just wrap this up before we test to make sure that our application can actually run by opening up a terminal here and we're going to run Maven clean install and we're expecting this to go without a hitch be a complete success so we have a build success it looks like our project has been successfully converted from a Java spring boot project to a cotland spring boot project so let's see if this actually runs so as mentioned earlier this project depends on a postgress SQL database so we need to run that luckily we have this Docker compose file here so we're going to say Docker compose and up and this will start up our postgress SQL database for us ready to accept connections and we're going to go to our main function that we we had from earlier this one here and we're going to click this little green tick and we're going to run our cotlin application and it started successfully so let's see if we can actually interact with this application let's bring up Postman so here we are in Postman if you haven't come across Postman before it basically allows us to interact with rest apis it gives us a user interface to do that so what do we have we know that we're running on local host on Port 880 and we have this author's endpoint post endpoint so what this is going to allow us to do is create an author in the database we also have various other endpoints so what we'll do actually is we'll take this we're going to paste this in and instead of using post we're can to use get and this is going to list all of the authors that we currently have in the database looking good we got 200 okay and we got an empty list so let's flip back to the create end point and we're going to create ourselves an author called Colin and he's going to be 24 years of age so if we send this away we can see here we got a 2011 created we have the ID of one we have Colin and is age 24 if we now click on the list endpoint once more we got our author Colin returned in the list so it looks like our cotlin spring boot application is working absolutely fine and there we have it a cotlin rest API all your own if this is your first experience of cotlin what do you think better than Java overhyped let me know in the comments below because i' be interested to hear now if you want to learn more about spring boot want to learn more about spring boot in Java in which case check out this full course right here because over 6 hours I'm going to take you from a complete beginner to being Prof with spring boot in Java I'll see you over there
Info
Channel: Devtiro
Views: 2,765
Rating: undefined out of 5
Keywords:
Id: qE7ar1XMaYI
Channel Id: undefined
Length: 38min 31sec (2311 seconds)
Published: Thu Feb 08 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.