Submitted by manoj on

Hello everyone, welcome back to techbrushup. In this tutorial, we will learn how to configure your vs code for developing this springboot application. So guys, let's check out the prerequisite first.


Additional info: Spring boot always starts from controller ie presentation layer

prerequisite:

Must have java jdk installed on your machine Officlal Java download link

Install Extension Pack

Step 1: Open visual studio and install some recommended extension as below.

  • Extension Pack for Java Extension Pack of (6)

    first spring boot application using the visual studio code

  • Spring Boot Extension Pack Extension Pack of (3)

    first spring boot application using the visual studio code

Create Java Project

Step 2: After the above extension is installed, you will notice that the 'create a Java project' button is now available. It was not there before.

first spring boot application using the visual studio code

Step 3: Now click on create a Java project and select the project type 'Spring boot'.

first spring boot application using the visual studio code

Step 4: Select the project type 'Maven Project' and hit enter.

first spring boot application using the visual studio code

Step 5: Specify Spring Boot version and choose latest top one.

first spring boot application using the visual studio code

Step 6: Specify project language and choose 'java'.

first spring boot application using the visual studio code

Step 7: Input group id of your project (press enter to confirm).

first spring boot application using the visual studio code

Step 8: Input Artifact Id of your project(press enter to confirm).

first spring boot application using the visual studio code

Step 9: Specify packaging type, choose the jar.

first spring boot application using the visual studio code

Step 10: Specify java version, choose 17 (at the time to create this article).

first spring boot application using the visual studio code

Install Dependency

Step 11: Search for dependencies and choose as per requirement ,basic are given below , you can see that Selected 6 dependencies

  • Spring Web

first spring boot application using the visual studio code

Step 12: Now choose folder to generate project.

first spring boot application using the visual studio code

Step 13: After sucessfully generated you will get a prompt, open it as below.

first spring boot application using the visual studio code

Step 14: Open file where main method is already defined and write print statement.

this print statement is for later use when you will run your project to confirm wheteher project run sucessfully or not.

first spring boot application using the visual studio code

Building The Application

Building and run the Application(So chill, No manual stuff required, Its automatically handled by VS editor)

Step 15: Here you can see the run option so just click on this run and run java and choose this hello application so let me just click enter so this will run our springboot application so you can see here this springboard application has been started and it started on port 8080.

first spring boot application using the visual studio code

Step 16: You can see here spring boot application has been started and it started on default port 8080.

first spring boot application using the visual studio code

Step 17: Congrats ! you have created your first project sucessfully.

first spring boot application using the visual studio code

Above high lighted message is same as you have already set in the main method of SpringBootApplication.

Restart and Stop

Step 18: to restart and and Stop follow below.

first spring boot application using the visual studio code

Step 19: Run your application on browsers localhost along with port no (http://localhost:8080/).

Step 20: By default if you see Whitelabel Error Page with status code 400,

first spring boot application using the visual studio code

It is expected Because, may be No route configure yet, its time to make your project functional....

lets Create front page route by setting two required properties


    			@ResponseBody
    			@GetMapping("/")
    		   

Step 21: Restart your application and navigate to browsers(http://localhost:8080/).

first spring boot application using the visual studio code

How to Solve Whitelabel Error.... 

Step 22: 

 

first spring boot application using the visual studio code

Step 23: Go on Java projects click on the three dots and then configure Java runtime or command shift p on a Mac or Ctrl shift p on a Windows search for Java configure Java runtime here I can see my projects types and the Java version if I don't have Java 17 installed already.

Step 24:

Now it's time to make your project functional

, Lets create 3 layer architecture project, Install dependencies as per Project requirement.

 

  • Spring Web
  • Spring Data JPA
  • Lombok
  • Thymeleaf
  • MySQL Driver

Step 25: Add DB configuration settings into application.properties files.

spring.datasource.url=jdbc:mysql://localhost:3306/ecommerce
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

first spring boot application using the visual studio code

Step 26: First open project under 'JAVA PROJECT' in VS Code.

first spring boot application using the visual studio code

As per Project requirement Add packages and correspoding their classes.

  • controller
  • entity
  • service

Step 27: Go to src/main/java\[project_name], Create first package (Please don't create in main root folder most of the people make a mistek otherwise will not work) by Click + icon in front of [project_name] Select package , input the package name (package name: controller), Press enter to confirm.

first spring boot application using the visual studio code

Step 28: Create first class under package by Click + icon in front of controller package select Java Class and input the class name (classname: Parentcontroller), Press enter to confirm.

first spring boot application using the visual studio code

Step 29: Need to import predefined classes for controller Press @Controller, it should highlight see below select and choose Controller.

first spring boot application using the visual studio code

Step 30: You can see dependency (import org.springframework.stereotype.Controller;) its automatically imported.

first spring boot application using the visual studio code

Step 31: Create first method 'index' inside first controller

If you want to map html for a particaular route first you need to remove @ResponseBody tag and under return statement map file name "index" for index.html etc.

first spring boot application using the visual studio code

Step 32: Create our first web page which will print the text “First Spring Boot Application Using The Visual Studio Code” on the screen for this Click + icon in front of templates (src/main/resources/templates) and Create index.html file.

spring boot.png

Step 33: write welcome message into your html file.

spring boot.png

Step 34: Restart required(ctrl+Shift+F5) every time if you want to check any modifications or changes you have done so far.

spring boot.png

Congrats !

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Standard (Image)