Step by Step Guide - VS Code for Spring Boot Java Development

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so as java developers we all love our ides right we cannot work on java without an ide we're so used to it and uh the most popular idea at this point is intellij almost everybody uses intellij eclipse is a close second and then netbeans some people use and swear by it never understood that but intellij is more or less the most popular one at this point today what i want to do is try a completely different program for java development and see if it works and the program i'm talking about is visual studio code okay how good is visual studio code for working on java projects does it work can we get work done with this thing and i'm gonna put this to the test not by writing a simple hello world job application right i'm going to actually build a spring boot service and by the end of coding this program we will see how good visual studio code is and whether we can do serious java development with it the results can be a bit surprising let's check it out so one of the reasons why java developers love ides is because none of us remember how to compile java code using the command line right you cannot be bothered to remember all the compiler flags and all that stuff ids make it very simple right you just click stuff right click stuff and you can build projects you have auto complete all that good stuff and we have come to rely on it as java developers so when you say okay i want to not use a full-fledged ide and i want to use something like visual studio code which is actually not a full ide the question comes is it even possible to work on this thing because visual studio code is kind of like a very popular choice for for front-end developers not so much for back-end developers so the question is how good is java development experience with vs code now you might wonder why am i doing this why would you even bother with vs code why not just to use intellij or eclipse the thing is well some of us are full stack developers we like to work on both the front and the back end so when you're working on front end you're very likely working on javascript technologies and you are using visual studio code and javascript and typescript for it so rather than having to switch between two editors right front end with vs code back end with intellij well why not try java with vs code and see how it works okay so what i'm going to do is i'm going to show you a clean install of vs code i don't have anything installed apart from like a theme that i've customized and now what i'm going to do is i'm going to add the required extensions i'm going to take you through the flow of building a java application with vs code and we will put it to the test we will see how good vs code does with a java development and again i'm not doing a simple project well it's technically simple but i'm not doing like a hello world one class project all right i want to do a spring boot application i want to do a spring boot service and the example that i have the scenario that i have is of like the easier up uh service i don't even seen websites like this like you can you can go to the website and say is facebook down or is it just me who's having a problem like is that website down or up so what you do is you provide a url to that service and it's going to tell you is that service up or not okay so you can say is it is it down for everybody or is it just me right that kind of a service so i want to build that kind of a service with spring boot and i want to do this from the scratch with vs code okay so let's see how this works so here is my vs code application open over here and uh i again like i said i don't have any uh plugins or anything like that installed it is bare bones it has this welcome page which i'm going to close and it allows me to open a folder now what i'm going to do is i'm going to actually create a new project but before i create the project i need to make sure that i have all the right extensions installed so the way it works with vs code is that you don't really have ide capabilities out of the box but it has this thing called extensions which kind of like plug-ins which you can install to vs code and have it do those things right you can add functionality to vs code by installing those extensions um this is the uh tab which allows you to choose extensions so if you click on this it'll show you what are all the uh available extensions what is installed and what is available okay so here if i can if i you know if i want to do python development i just type python and here are all the python extensions okay so i can get javascript extensions there are a bunch of different extensions we do a lot of stuff but what we are interested in is java okay so i'm going to search for java and the most popular one and this is something that i would recommend if you're going to try java development in vs code is this thing called java extension pack okay vs code has a concept of extension packs which is basically a bunch of different extensions bundled together and you click that one install button it's going to install all those extensions okay so here are all the extensions that are available so you have visual studio intellicode which is basically a ai assisted code completion i know ai assisted is a buzzword everything is ai these days i don't know how good ai really is apart from the good non-ai code completion but it is the code completion uh option that you have over here right so you have language support for java you have java linting more intellisense uh java debugging maven support test on our support project manager support there's a whole lot of stuff that you get out of this so what i'm going to do is just click install okay one good thing about vs code is that it's free okay so no paying for uh jetbrains to get intellij enterprise edition you get some of these features for free so that's another factor that's involved in this comparison okay so you have you're basically comparing a free tool with a paid offering so let's see how this stands okay so i have installed this and now it's uh it's done all right so i have java support now what does this mean we'll find out in a bit but there is one other extension that i want to install which is spring support because we're going to be building a spring boot application if you're not using spring boot or any of the spring technologies you may not want to install this but since we are doing a spring project i'm going to install spring extensions okay so i'm going to just search for spring in this extensions tab and now here you see there are a bunch of stuff here spring boot tools well here is something that's interesting spring boot extension pack again this is another extension pack which contains a bunch of these um you know extensions again i'm going to just click on install okay so it's going to install those stuff wait for it to complete and we're done okay so now i'm going to create a new java project you notice here now there is a create java project button this wasn't there before right now it's come up so what we're going to do is i'm going to close this guy over here and instead of creating a new java project i want to create a new spring project and this is where the first one of the extensions that we've installed comes into the picture so how would you create a new spring boot project you go to the spring initializer right you go to star.spring.io and you configure this well with the spring uh extension pack installed in vs code you can actually do that in vs code and you don't have to go to the website okay so here's how you do it first thing you do is you open the command palette okay vs code has this concept of a command palette you do that by typing shift command p or in windows i think it's ctrl shift p i'm going to put that in the video over here to tell you what the windows shortcut as you must be seeing this over here right now okay so this is the shortcut to open the command palette now what you do here is like you get this text box and you have all the commands that are available this is a whole lot of commands it's not going to show you everything here but you can actually type stuff and it's going to suggest what are the commands that are available okay so if i were to type spring i'm going to get a bunch of stuff here uh there's a spring boot dashboard you're not there yet we have spring initializer see there are three of them okay add starters create a gradle project create a maven project i'm going to choose the maven project this is basically the equivalent of going to start.spring.ion saying hey i want a maven project okay so i'm going to hit enter now it's selected this and now it's asking me the next question okay it's kind of like a wizard format okay specify spring boot version okay i'm going to specify the latest stable one which is 2.4 2.4.1 again it's selected i can use arrows to select something else and then hit enter project language i'm going to choose java and then the group id and then the artifact id okay so the artifact id i'm going to say is the site up okay so that's the that's the scenario that's the application that we're building we're building an application that's an api and then you give it a site url it is going to make the request and check if the site is up okay so i'm going to hit enter now what is the packaging that i need i'm going to need a jar okay i want it to be a smart jar java version i'm going to choose 14 and now i get to choose the dependencies you remember when you go to the spring initializer you have this option where you can choose what are the dependencies that you need for your spring boot application so here are all the dependencies okay now i'm going to choose a bunch of these stuff so what i need to do is again you know move to the thing that we need using arrow keys and then hit enter okay so spring boot dev tools enter so it's selected you notice here it says selected one dependency what else do i need i need spring web hit enter selected two dependencies and that's it i think i'm done i don't need anything else so i'm going to say here here it is they selected two dependencies press enter to continue i'm gonna press enter to continue enter and now it's telling me where do you want me to save this project okay so i'm gonna give it a directory i'm gonna call this uh code and i'm going to generate into this folder so basically what it's going to do is it's going to generate that spring boot app it's going to download it from start spring.io and then it's going to put it there okay so now it has given me this prompt over here it says successfully generated location is this the code directory that i've added and now it says you want to open it all right i'm going to click on this and it is going to open this project over here okay there are a bunch of other welcome windows that are open so i'm going to close these two welcome windows those are from the extension that we've added okay and now here there are a couple of prompts okay so this one says the workspace oops it went away let me click this here to bring it back up okay the first one notification says the workspace contains java projects would you like to import them okay the second one is maven wrapper is found in this workspace the workspace do you trust it okay first of all yes i want to import the java project so i'm going to click yes i could also have said always so that it always imports java projects uh let's look at this prompt now maven wrapper is found in this workspace do you trust it okay so here is this name and wrapper mvnw okay in case you don't know maven wrapper is basically this this uh script file that kind of wraps maven so that you don't have to install it okay so let's say you you get this you're doing this whole thing but you don't have mayman installed okay how would you run this thing well you're going to go get maven put it to the path and then install it and all that stuff you don't have to do this but this maven wrapper what you do is you can just run this command and this is going to take the place of an installed maven okay so you have this mvnw another mvnw.cmd for windows these are wrappers for maven so that you can get maven out of the box and you don't have to install it the problem with this though is that this is a script okay it's a shell command and vs code is asking hey this is a shell command which can do anything from starting maven to wiping out your whole hard disk because guard knows what's in that shell command in the shell file so it's asking do you trust it since i downloaded this from the official like downloaded this via the official spring extension i think i trust this thing so i'm going to say yes i trust it so i say yes now here is another prompt that it has uh do you want to exclude the vs code java project file settings like uh classpad.project.settings from the file explorer basically do you want me to show you those files in the file explorer which is over here i don't want to see it so i'm just going to say exclude globally so that it it excludes it all over the place okay it excludes across the board for all projects you could also say exclude in workspace so it is going to ask for this every time okay every time you open this java project it's going to ask you and then you can say never so that it never gets excluded and you always see these dot class path and um dot settings and all those files okay so i'm going to click on exclude globally you can choose whatever you want but now you notice here those files are not visible they're still there they're just not visible okay so i'm going to close this maven wrapper file and now here is my spring project now if you notice this doesn't look really like an ide so you have all these files over here it's like yeah this thing does a nice collapse but then it doesn't look like it's a it's a package right so this is where you have another view that is super handy which is this view called java projects okay so if we expand this this is more like and i'm going to collapse this view here okay so this is more like what you're familiar with in intellij so you see here this is the project is the site up and now here i have the src main java put together src main resources put together src test job over here here is the system library that we have we have asked java 14 so that java library is over here and then here are all the main dependencies that my spring boot project contains and guess what it actually downloaded them all i didn't have to do anything you just picked them up and made them available over here okay so it is in the home folder dot m2 this might be in a different location depending on your even configuration but this is it so now i have the project set up and ready to go okay so so far i would say this is pretty much the same as an intellij experience except for these prompts because i'm kind of starting from a clean slate if you chose those always options for those prompts you wouldn't get asked those prompts again so opening and creating a new project would be fairly simple and i would say this is actually a step up from the intellij free edition so opening a spring project uh creating a new spring project is not possible in the community edition of intellij unless i'm mistaken here so if i'm mistaken let me know in the comments and correct me but as far as i know you need the paid version of intellij to create spring projects okay from start.spring.ios this one's a free solution and it's i think it did well so far right so let's see how the experience development experience is like so now i am going to open this class which is called um is the site up application so this is a java class i'm going to expand this just a little bit more and what this does is well actually i don't want this whole thing to be zoomed out so what i'm going to do is open the code preferences preferences settings and in my text editor i'm going to change the font size here a little bit okay here's the font size i'm going to make this 16 so and then press save this is going to increase just the font size and not the size of these uh icons and the labels and the menus and all that stuff okay so when you open this java file you notice that this is a springboard application you have the springboot application annotation here which is this is the main class when you have a main method here you notice there is one other thing that vs code does for you it has this run debug option okay this is what's referred to in vs code patterns is code lens they call it code lens the idea is that there are certain areas in the code where you have these actions you can click on it and it's going to do some stuff okay so here is this run link i can click on it and it's actually going to start to run this main method okay it detects that it's a java main method so it knows how to run it okay so here's what happens let me click this run okay i'm going to click on run and it is compiling it's doing some stuff and then here is the terminal and here is my spring boot application up and running awesome okay so this wasn't so bad either all right so here is my uh localhost colon 8080 all right so this is the this is the port where the application is running so i have this application up and running so that's kind of awesome so let me actually switch to uh the browser and make a request to make sure i get the error page the classic white label white label error page that spring boot has okay so here's my firefox window i'm going to do localhost colon 8080 and yes i get the white label letter page so our application server is up and running okay so you don't need to remember the java compiler flags even when you're working on java in vs code so it's been it's been fairly good so far but the real test is when we actually write code okay so let's see how this performs so what do i want to do i want to create an app which takes in a url okay and it checks it makes a ping to make sure that that url is up so the first thing i need is a controller okay so let's see if there is a right click create controller well there isn't a controller right click option okay so this doesn't know that this is a spring boot application it doesn't have an option of creating a controller but it has an option of creating a new java class okay so let's do that and now here it gives me the same text box right this is the standard uh ui pattern in vs code like input the class name so let's see if a package name works i'm going to call this controllers dot um url check controller okay so this is the controller which is going to check for a certain url and notice here the the package name is coming up over here it's not just the class name okay it says controllers dot url check controller i'm hoping it's going to create a package and then put this class in there uh which it did all right good so far so it's created the controllers package and put this class in here and now it gives me an option of saying okay do you want a class or an interface or an enum or a record or an abstract or ad interface like an annotation so it gives me a bunch of options it's uh it's pretty modern it has a record here already okay so that's cool so you have class interface enum record abstract class or an annotation so what i want is a class because we're creating a spring mvc controller so i'm going to say class url check controller okay good so far now what i want to do next is to make this a rest controller because i'll be making this like a rest api for this they can pass in a query param which is the url that i want to check and it's going to tell is this site up or down okay so i'm going to make this a rest controller and now this is another test okay let's see if it can identify that this is an import okay so i need to import this rest controller and how easy or hard is it so i'm going to say at rest it finds it all right cool rest controller from our spring framework web okay so i can click on this expand it shows web bind annotation risk controller yes this is exactly what i want okay and now i have marked this as a rest controller and now i need a method okay so this is going to be a get method i'm going to call this public and uh boolean right as a site up or down or maybe it's yeah i mean let me make it a string so that i can basically send the message because there could be other cases as well like the url is informed well you know 404 whatever else okay so i'm going to call this is url or maybe get url status message okay which is what this is and i'm going to take as an argument a string url and what i need is let me close this guy here what i need is for this to make a ping and identify if the site is up or not okay so uh first of all i need to make this a get mapping okay and um let me put this a url slash check because i'm checking if this url is up or not so it's going to be a slash check and um let me make this a query param okay so i'm going to put the url as a query param and the way to do that is by using at request param autocomplete seems to be working fine so far all right so at requestparam string url did it have the string did it remove it that's interesting yeah it had the string and it removed it you see here request param the string goes away that's hard i don't know if it's a bug but that's probably the first negative mark i'm going to give to this guy okay so uh request param string url so no now i have the string url what do i do with it now i need to make an http connection okay make a get request and see if i get a successful response that's what's going to be the the the logic of this method all right so i'm going to create a couple of um constants over here private final string uh i'm going to call it like a bunch of messages right what are the messages i want to show depending on what the condition is so if the site is up i'm going to say yay site is up site is down it's like no so is site up equals slide is up okay now i'm also going to do a this site down actually let me make this it's not a question it's an answer site is up side is down okay and now how what i need to do is have like a string return message is an empty string and based on one of these things i'm going to assign one of those constants to this thing and then return it at the end okay so in the end it's going to be returned return message okay so now how do i check this well the first thing i need to do is to make uh create this url thing and then send it to http url connection all right so i'm going to do a url i think it's capital url yeah java.net url okay this is what i need so i'm going to hit enter and uh every time you do this what what this is doing is like you would expect it's actually importing it so vs code is actually the the extension the java extension is actually importing it so you don't have to you don't have to do it manually which is cool okay so you have the url object equals i think there is a constructor uh which takes in yeah there is a constructor which takes in a string you see this here so this is what i need so i'm going to do a url of url okay now i get an error here what's the error what's happening ah it throws malformed url exception if i mouse over it says unhandled exception type malform url exception okay so there is a quick fix link over here what happens if i click on it i get an option i throws declaration or surround with try catch i'm going to surround with try catch because i don't want to throw a message uh throw an error if there is a malformed exception i'm going to show a message which says url is incorrect okay so let me actually create that uh thing as well over here so i'm going to be like let's call this incorrect url and the message is going to be url is incorrect i didn't mean to shout sorry caps lock i pressed it by accident is incorrect okay so to do auto generated catch block thank you vs code for giving me that but i don't need it right now what i'm going to do is return message equals incorrect url okay so now i need to open a connection i think that's h http connection let's see connection http url connection okay so it's pretty smart and it's auto complete so that's cool http url connection connection equals url object dot and there is an open connection here okay so i'm gonna have to start a new connection now this gives me an error what does it say it says type mismatch cannot convert from url connection to http url connection i think this needs a cast so if you do quick fix add cast cool so it's not bad so far it seems to be doing everything that i would expect a proper java ide to do so this is pretty cool it's actually showing one more error message okay unhandled i o exception okay quick fix again maybe add it to okay now i have an extra option right i can add the cash class to the surrounding try or i can add an exception to the existing cache class which is what i prefer i can also surround with the try catch which basically puts another try catching nested try catch or i can just add the throws declaration to the method itself i like i like this one right i want to add to the existing right well no actually not what i wanted i actually wanted uh add a catch cluster surrounding yes this is what i want so in this case if there is an io exception what does it mean this means that the site is down okay it's having a problem with the io so what i'm going to do is i'm going to return the site is down message okay so let's say return return message equals sight is down okay and uh when this works so here i'm going to do uh the connection is open now i'm gonna have to do a get call okay so i'm gonna do a connection dot set um request method all right this is exactly what i need this is probably the ai thing how did it know that this is exactly what i need right see you see this i do a connection dot and the first thing that comes up is request set request method okay so this is either something that it's noticed the pattern of people coding they get the connection and then the immediate next thing they do is to set the request method or it's just fluke i don't know anyway really cool that the first thing came up all right so i'm going to say a set request method and then what is this guy is this a string it is a string so i'm going to set a string get okay so i've said the request method and now i need to connect i'm going to do a connection dot let's see if connect comes up first no it did not all right so this is probably not ai it was probably just fluke so if it really had a i would have said connect here and then uh disconnect afterwards anyway so i'm gonna do connect and then uh now i'm going to once it's connected and it hasn't thrown any exception i need to check the response code as well so what if it successfully connected but the response code was like a 404 okay that's not good so what i need to do is i say connection dot get response code if this is equal to 404 actually let me make this like if this is as long as it's 200 it's okay if it is 404 or any of the 400s or any of the 500s that means that that's an error right and the site is down so that kind of makes sense so what i'm going to do is uh check if it's between 200 and 299 i'm just going to divide by uh 100 all right so if oops if connection.response code divided by 100 is equal to or maybe if it's not equal to 2 that means that it's not any of the 200 response codes okay so in which case i'm going to mark it as down or maybe i can do uh three as well so even three as is allowed so let's say um int response code equals actually let me call it response code category probably a bad name but you get the idea so what i'm going to do is put the response code category over here if the response code category is not equal to 2 or response code category not equal to 3 that means that the site is down okay so i'm gonna say return message is that the site is down okay and else return message equals site is up and this is this is pretty much my code this is about it i think and then here at the end i'm returning the message okay it's pretty cool now since we have added um dev tools this should do a live reload so what i'm going to do is go to my browser and then here i'm going to do a slash check and then i'm going to pass url as an argument right url equals let's say let's see if google is up okay https colon double slash www.google.com okay that didn't quite work let's see it says 404 let's see if it's why it's not seeing this controller i have marked this wrist controller ah this is why controllers has gone into a separate packet it should be under under this so let's see if drag and drop works can i drag this and put this over here yeah drag and drop doesn't seem to be working can i drag the class that doesn't seem to be working either so now what i need to do is maybe change this guy over here to say io dot is the site up dot controllers okay it has to be within the iot or java brains uh that is the site the package otherwise the spring scan is not gonna see it so now uh can i move this thing move okay so this works better now this thing is gone let's try refresh there you go okay so controllers has gone inside this package so that was a problem so let's try restarting this again i'm gonna go to the main application and click run all right so i'm gonna go to my browser and uh type the url slash check and again another thing string parameter url is not present okay so let's add that i'm going to check if um google is up okay so https colon double slash google.com and it says the site is up and uh if i make a mistake here it doesn't find it all right goggle.com is not available say site is down okay and uh let's do um java brain start io you gotta do a bit of self plug right java brainstorm io is up yay all right so let's find a website which is uh which is down and uh it still responds with the url let me go to github and uh explore some some repository let's go to topics cradle or maybe training okay so this one's trending which is fine so this particular url should be available okay url equals put that url that site is up but if i type some nonsense here it should not be up it shows a page all right it shows a 404 but we should be able to catch that 404 and still say site is down in spite of us actually getting a response so i'm going to type this url hit enter and it says site is down like you'd expect so cool our application is working fine and uh notice what we did we actually did this whole thing without really doing a lot of the manual steps that you would do if you were just using a plain editor right we didn't have to import stuff it had auto complete it had an ability for you to actually start a project from the scratch right in the editor itself which i think is really cool this actually surpassed my expectation of how vs code would work for a java project it's been pretty cool and i don't see it as being less than intellij in what we've done so far it's been pretty cool what is this it's an unneeded import quick fix remove unused imports all right cool so it seems to be doing everything that i would expect uh an ide to do uh there are a bunch of other things that you can do with this thing for example uh here are maven dependencies i can actually add a maven dependency by clicking on this plus okay well let's do a search for apache commons um it's going to tell all the uh yeah this is probably not started in order of popularity would be a good thing to have apache commons and at the top pretty sure these ones comments by srijan kujmar is probably not as popular as apache commons so this is probably doing like a last updated sart or i don't know if it's it's not alphabetical for this as well so i don't know how it's ordering this but it would be nice if it had a good um starting based on popularity let's say apache commons core okay so this is better now it tells you the exact uh dependencies that you need so what this would allow you to do is basically choose one of these and let's say common jci core okay so i hit enter it basically puts this automatically it's pretty cool isn't it so you have a bunch of these things uh that you can do it's uh i think it's it's a pretty good program to work on uh work on using uh using java and um yeah pretty happy with how it's turned out uh there are a bunch of other integrations with like for example git so here is uh the git integration you can initialize the repository here which makes it like uh you get source control over here and now here are all the changes that you have uh pending let me save this guy over here and i'm not going to save form.xml because there was just some dependency that i added didn't mean to do that so here now i can say uh first comment and then press this thing and then it says okay there are no stages uh changes to commit you want to commit everything and say always come at everything and then okay make sure you configure your username and user.email and now i can do that over here and once i do that i'm not going to do it right now but basically once you configure that this is going to work seamlessly all right you just press this comment button and it is going to commit and then you can um you can push you can pull all the stuff happens right here so i really don't want this to be a tutorial for vs code i was just trying to explore how vs code works for java development uh we did a very simple uh java project using vs code and um i would say thumbs up give it a try uh let me know in the comments if you want to try a vs code if you've already been trying vs code how has it been any problems that you've had or if i've maybe convinced you to at least give it a shot let me know in the comments what you think i hope you found this video helpful thanks for watching
Info
Channel: Java Brains
Views: 84,307
Rating: undefined out of 5
Keywords: java, java brains, tutorial, brains, koushik, kaushik, explained, programming, koushik kothagal, beginner, java tutorial, vscode, visual studio code, vscode java, vscode spring, spring boot
Id: uq4GjRF_860
Channel Id: undefined
Length: 39min 56sec (2396 seconds)
Published: Fri Dec 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.