How to create an REST API in Spring boot using Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey it's random code here and today on a circle is how we can quickly and easily create an API in Java using spring boot so I will just first create a new project and we use a spring initializer let's call this project spring API you're going to be using Java I'm going to be using Maven group fine that fit fine I'm going to be using Java 17 which should be fine and four dependencies the one dependency we're going to be needing is spring web so I would just add a spring web finish and let the project load so we now have very simple spring based project and it might take a few minutes for you for it to load the first time or maybe to get all dependencies get everything set up but otherwise we will then have a spring API folder with most importantly our source main Java and then inside comic sample spring API we have the spring application which is very spring setup for a basic spring build application that then starts our opposite white main from this position so to then create our rest API I would add a few folders just to create a basic structure so I would create an API folder and I will create a service folder because I would be using the basic structure of the API and then have them had a service handling kind of like the back end setup for this API inside my API folder I would then also have another folder on the column packages it's not the same thing but I would then have a controller and I would have a model not like that let's actually take a look at the project files instead and I would also have my like that so inside the API package I have both my controller and my model package so inside my controller I would create a new Java class and let's say in this case we are building a base API to get some user data so I might creating a user controller outgrade our model for our user and inside Services I will create a user search we would then create a very simple user so let me just quickly construct a user and here we go so now I have very basic user I have a user ID a name an age and an email I have constructed singing all these parameters I have Getters and sellers for all of my values so what I wasn't very simply do is inside my use a controller I would then highlight this user controller as an rest controller and if you just add old sex we have a rest controller with address controller and I would then also for my service just add it as a service but then very simply inside my wrist controller I would then create a public method so I'll do pop look and then I would like it to return in this case just a user and I'll just call it get user example and I would then make this method into an get mapping which means we can make git calls to this API setup to get this information and I would then in this example just very simply create a request parameter which is kinda the inputs we give to our API to sell in this case which user would like to get so I'll do a request parameter where I would do it as an integer ID we would then just very simple inside this method just return some user based on some ID and I would then create my service so I would do a private user service user service I would then use construct injection to do you have to set up this service so I will do a public user controller taking in uh user service you know service and then my list.use service equaled my user service and I will then add an ad Auto wide for the short injection to then inject my usual service service into this controller and I would then simply here just return my user service Dot get user based on the ID and of course we haven't created this method yet so I would create method in user and now we're getting to a point where it's not very realistic because normally you probably have this in this service connect to some kind of database and then actually extract some visuals might have some SQL queries to kind of simulate this database setup I would just in here create a private list of users called use list and our then inside our empty Constructor just create a new use list equals new arraylist I will then create a few users so do user user user equals new user and I've got an idf1 the name of Theta an H of 32 and an email of either add mail Dot com and let me just add a few more users like that so I now have five users with different IDs different names different ages different emails I then add all these users to my user list I would then very simply whenever I get this get user call to my service I would just simply go through my I'm just going to single for Loop that's very simple so we go through our so user user in I'll use a list seem to do if the ID from the input is the same as our user.getid and would you seem to return this user otherwise we would return something and then actually there's quite a few different ways we do this a bit more optimal yeah let's actually just do it properly so instead of actually returning a user here I would be returning an optional optional containing a user and I would then return an I would create a new optional so optional optional equal an optional dot empty we would then instead of just return that user we then set our optional to be equal to optional Dot of our specified user Android and return our optional and in case we actually never hit any other optional or we don't hit any user I'll just return my optional like that and in here instead of just turning this user because it's not going to be a user is going to be an optional I would then have my let me get an optional usually equal to our user service.get usually I mean then simply say if our optional or if our user dot it's present you then return power user Dot dot get and then cast it to a user otherwise we would return let's just return all and this is definitely not the best way of doing it because normally we probably wouldn't just return a user directly with problems return some kinder entity with some kind of network code so we can Define that in case the user's not present instead of just between the user we would return a specific code so we might return content that found or something like that but now I think is just simple setup so it should be fine so let me actually just open Postman so we can actually check this works so we now have Postman running and one last thing instead of just have our get mapping we run on default that actually add a path so just add user so now when you run the program if I didn't make any mistakes we should be able to have our API running on localhost port 8080 as we see here we can now do localhost port 8080 slash user and we can then pass it a query parameter for ID and let's just try doing a D1 so here you can see the actually API call we're doing so localhost port 8080 slash user with the parameter of id1 and as you can see here we actually end up getting our id1 which is Ida 32 and with the specified email and if we go back into the service we can also see that it is actually matching correctly and then just to check it works let's change and let's do five for example we now get by either 9 which matches let's actually check the setup we did without any users so for example six in this case we just return null which ends up returning nothing which is not perfect and definitely not the best as mentioned we might actually want to change instead of having to respond to status be 200 okay you could probably be I I don't remember all the numbers in my head but I think at least there's something called like partial data found something like that so I actually showcase that something's not here or maybe you would actually just like want it like this where we just have a setup where if you try to request something that's not there return nothing because that's also kind of logical and makes sense but this is the entire setup of our spring-based API in Java which I think is a very simple and very quick setup where we actually have a table controller a model and a service and we then Simply Connected through string using so basic spring principles like for example inside our controller we did construct injection instructing our service into our controller then simply call it we use an optional which allows somebody check as long as the user present between the user otherwise return not return nothing so I think this is a very good Showcase of a very powerful tool or very powerful Library spring boot which can be used for creating some very simple and very nice rest apis with plain Java so if you enjoyed this quick showcase or relatively quick showcase please leave a like And subscribe and I wish you all a wonderful day
Info
Channel: Random code
Views: 26,610
Rating: undefined out of 5
Keywords: random code, client, how to create rest api in java using spring boot, java, json, postman, rest, rest api, rest controller, spring, spring boot, spring boot tutorial, spring boot rest api tutorial, learn spring boot, spring boot rest api, java spring boot, spring boot tutorial for beginners, spring boot microservices, spring framework, spring boot java, spring boot project, how to use spring boot, netflix and spring boot, spring boot full course, spring boot tutorial for begineers
Id: Zo9xQzibp4Y
Channel Id: undefined
Length: 12min 26sec (746 seconds)
Published: Mon Oct 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.