Spring Boot and Visual Studio Code

Spring Boot is a Java-based tool or Java-based framework for fast and easy web and microservices development. With Spring Boot, you will build stand-alone, production-grade Spring based Applications with minimal configuration. 
by embedding a web server such as Tomcat or Netty into your Spring application during the initialization process, you can launch your application on any platform without relying on another external web server.

To get start with a Spring Boot app, you need JDK (Java Development Kit) and a code editor that help you manage, debug, and run the application.
You can use Eclipse wth Spring Tool Suite plugin or Visual Studio Code. On my machine, i have Visual Studio Code with the following plugins:

Spring Boot Tools
Spring Initializer
Spring Boot Dashboard

You can download and install those tools into Visual Studio Code from this link.
To create a Spring Boot project with Visual Studio Code, from the top menu, select Help -> Get Start. On the Walkthroughs, select More.... Then, you select Getting Started with Spring Boot in VS Code. This will help you open Spring Initializer that help you through the process of creating your Spring Boot app.


Click Create New Spring Boot Project. Then, you will be able to select Spring Boot version (2.7.4), project language (Java), package name (com.dev), app name (spring-tutorial), java version (8), package type (jar), and dependencies (Spring Web, Themeleaf, and Spring Boot DevTools). Here is the result configuration file (pom.xml) after the project initialization completed:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.dev</groupId>
	<artifactId>spring-tutorial</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-tutorial</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

In the com/dev/springtutorial folder, create MvcConfig.java file that will load home.html file when the root url is accessed from browser:

package com.dev.springtutorial;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("home");
		
	
	}

}

Create home.html file in resources/templates folder. Here is the content of the home.html file:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Spring Boot Tutorial</title>
    </head>
    <body>
        <h1>Welcome!</h1>
        
        <p>Click <a th:href="@{/hello}">here</a></p>
    </body>
</html>

Now it is ready to run the application. In the com/dev/springtutorial folder, select SpringTutorialApplication.java. Select Run - > Run Without Debugging (Cltr+F5) from the top menu. 
Note: If you get Unresolved SpringBootApplication error, restart Visual Studio Code and run the app again.

By default the application run on port 8080. Thus, after the app successfully started, visit http://localhost:8080/. You will get the output as below:


If you click the here link now, you should get Whitelabel Error Page because we do not do anything to hand the hello path requested (http://localhost:8080/hello).
In the com/dev/springtutorial folder, create controllers folder. Then create controllers/HelloController.java and place the following content:

package com.dev.springtutorial.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
// Annotation
@Controller
 
// Class
public class HelloController {
    
    @RequestMapping("/hello")
    @ResponseBody
 
    // Method
    public String helloWorld(@RequestParam(name = "name", defaultValue = "World") String name)
    {
 
        // Print statement
        return "Great!, Hello "+name+"!";
    }
}

Save the change. Wait, the application is reloading. By adding spring-boot-devtools dependency, the application will be reloaded automatically when you have changes. Thus, there is no need to run it again.
Now click the here link again, you should see the output as below:


In the helloWorld() method of the HelloController, we defined name parameter. Default value of the name parameter is World. So, you see "Great!, Hello World!" message when accessing http://localhost:8080/hello. Now let access http://localhost:8080/hello?name=Dev. We get the "Great!, Hello Dev!" message.

Comments