Quickest Way to Create REST API in Java with Spring Boot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is running from developer soapbox and today I'm going to show you how to create a restful api using spring boot and specifically spring data and we'll do this in like less than 10 minutes if you're familiar with other frameworks like rails and you know your rails developer friends are telling you how much easier it is to create in using that framework or Python using flats you know you can definitely show them off using this particular setup so let's get started so the first thing you're going to do is open up your browser and go into start dot spring the i/o and this will allow you to generate a spring boot project in only a few steps you can actually choose what type of build tool you want to use even what kind of language you want to use and then the specific version of swing boot in our case we're gonna use maven will stick with Java and we'll go ahead and use spring boot 2.0 I'll leave the group and artifacts as is and then here you can actually specify the particular dependencies you want to use in our case I'm gonna use JPA and restless repositories rest repositories how browser and I'll show you guys at the end of the video what this does and this is pretty awesome and then we'll use an h2 database to set this up I mean this is kind of a testing project if you're doing kind of a real project you probably use my sequel or Postgres but for for now we'll just keep it as h2 and go ahead and click generate project to generate that now I'll go in to the location where your browser downloaded that zip file to and go ahead and extract it and in my case I'm going to be using Eclipse to open this project specifically the spring to suite which is simply Eclipse with the spring plug-in so if you just go file import and this will be existing maven project and then just go ahead and browse to that directory my case it's downloads and demo and hit OK and finish and it should be importing all that good stuff in and we're going to be doing for this particular example is we're going to be creating an API to update a database with our favorite beers very simple you know similar to a to-do list but since every single example out there already uses it to do lists I figured I do something a little bit more fun and different and since this will be a list of our favorite beers for the first thing we're gonna need as far as code is concerned is a entity - mapped or beer stable so I'll just go into the generated package here the new class and my class thing would be R hit finish and this will have three properties first one is an ID second is a name for the name of the beer and third is a double for the ABV which is the alcohol content and let's go ahead and generate getters and setters so just go to source and generate getters and setters for everything and there we go so now we just seen a few different annotations to make the JPA work first thing is go ahead and add in an enemy annotation to our at our class level and this basically just tells JPA that this is indeed an entity that maps to a database table of the name beer second is go ahead and make tell JP a that this is our ID for the record level on the table and then since we don't want to generate the ID ourselves go ahead and add a generated value annotation to the ID and this will basically just make it so JP a generates the values for this particular field for us now that we have our entity class the second thing we need is the actual repository basically the class that will manage any any crud operations to this particular table so just go ahead and in interface and I'll show you guys why it's an interface in just one second and we'll just call this rest repository okay and what we're going to do is make this repository extend a string repository which is crud repository and for the grub repository you just have to specify the particular class that this repository operate on which in our case is beer and the type for the ID of beer which if we go back to beer and then we just need to add one annotation to this interface which is repository rest resource okay so two things about this interface first thing is by extending crud repository we're essentially getting a bunch of different crud methods for this particular entity so things like you know a find by ID and post in the lead and so on all of that is already done for us we have behind the scenes by string second thing is repository rest resource this annotation will basically create the API for you so essentially once we start off this project it will generate the API based on you know just a set of sensible defaults and we'll see that in a second but before we start our project there are two less things we need to do first is a set up or database connection and then we'll add some simple data to that so if we won't into our source main resources and open up application properties and here just to basically tell spring food what the water data source is we're just going to add one property which is spring dot data source dot URL and since this what we're using h2 the URL is JDBC h2 and there's some memory database so this is how you by that and then the name of our database we just call it beers and then to actually seat some test data in or we're gonna do is create a new file within the same directory so just a standard file and this has to be called data dot sequel by convention and I'll go ahead and open it within the same that ID for you guys and then what I'm doing here is that I went ahead and just paste the code so you guys don't have to watch me type this out I'm just I just have some DML on here to add three records into my beer stable and as you can see we don't have to specify the ID since that is a generated value so now let's test it out let's see if this works so one thing that I do want to mention in case some people might be confused like what where is the database right so since we're using h2 and this is a we specified here that it's an in-memory database what's gonna happen is whenever we actually start off the project it'll the the program will actually create the database generate the actual database table and and seed in the values of this data dot sequel and like I mentioned this is based on convention so simply by having a file name data dot sequel within our resources directory the spring boot knows to look into that file for any C data but before I start off the other project there is one issue that I caught which is I did not specify a strategy for the value generation so let's go ahead and specify that so strategy here should be generated type dot identity okay so to actually run the application go into demo application like Java and run that and there are your servers up so open your browser and go into localhost 8080 and like I mentioned spring boot has tons of sensible defaults similar to something like Ruby on Rails so the default here is since our entity name is beer it'll basically just use the floor of that so if I go to beers there we have it there at least as far as I get is concerned this is it's returning JSON right so we have a collection of beers and for each one will have or beer object right so there is basically that that's the data that we seeded within our data that's equal file so you're probably wondering how I'm going to show you how to do like posts and deletes right so I could since I am using Linux here I could use something like curl but this is the really cool thing about again using spring boot is it actually provides you with a browser something similar to like a soap UI or something along those lines so if you just go to localhost 8080 slash slash index dot HTML this is the page that's gonna come up and it'll have basically all your resources right so here I mean you can do a get which is basically what we just did by calling the URL directly but you can also do an on yet right so here let's do a post right which will insert a new one and if I do say oh it'd be you know strong beer some random strong beer and if I do posts make the request to go back here to refresh there's there's a beer right if you wanted to actually delete a record same thing so go ahead and actually let's go back to index go to beers nan get and we'll do a delete for my ID right so let's do beers one make the request and if I go in here you deleted my first record which was for highlight so there you have it I mean I know a lot of people will probably say well you know you said this is as quickly as Rios but you know the rate of scaffolding will provide you a lot quicker functionality well you know keep in mind that whenever you're setting up a rails project you before you can do scaffolding you also have to specify stuff like the database connection properties and so on which I did do with in this example but I mean as you saw just by simply creating a repository with few lines and just one annotation or able to create an entire API you know which which is pretty awesome so I really hope you have liked this video if you did go ahead and hit that like button if you'd like to basically receive a notification every time I put a new video out go ahead and subscribe thank you very much
Info
Channel: Developer Soapbox
Views: 73,521
Rating: undefined out of 5
Keywords: rest api, java rest api, spring boot, spring data
Id: Of1IcjpGNNg
Channel Id: undefined
Length: 12min 35sec (755 seconds)
Published: Mon Mar 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.