1. Installation
- Install Java and Add
JAVA_HOME
in env - Install Maven if you have not
- Install Java Extension Pack
- Install Spring Boot Extension Pack
- Install Maven for Java
- Install Project Manager for Java
2. Open settings.json VS Code
Add the following:
"maven.terminal.customEnv": [
{
"environmentVariable": "JAVA_HOME",
"value": "C:\\Program Files\\Java\\jdk-17.0.2"
}
],
"java.home": "C:\\Program Files\\Java\\jdk-17.0.2"
3. Generate starter pack
- Download a maven java project from spring initializr with Spring Web dependency.
- Or you can VS Code Spring Initializr extension to generate new Spring Boot projects. Type
Ctrl+Shift+P
and searchSpring Initializr
.
4. mvn install:
If needed, close and Reopen your VS Code after configuring the settings.json
mvn clean install
5. Add basic rest route:
You may refer to the Spring Quick Start Guide and copy the code:
package com.example.testapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication .class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}
6. Run Springboot application
mvn spring-boot:run
7. Visit localhost:8080
You may try http://localhost:8080/hello?name=yourName
8. Add more dependencies
Go to pom.xml
file and right click > Add Starters
9. Build an executable JAR file - Build Once Run Anywhere
A single executable JAR file contains all the necessary dependencies, classes, and resources and run that. Building an executable jar makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth.
mvn clean package
This will create a target folder. Now you can simply run to run the app as well:
java -jar target/<project_name>-0.1.0.jar
Jar file can run when double-click too. Just make sure to associate javaw.exe
with jar
file.
10. Can use BAT file instead
If .jar
file does not work. You can create a .bat
file. The batch file can be executed to invoke the jar, this method should definitely work. Add the following:
java -jar <project_name>.jar
11. AutoGenerate Getters & Setters
Install Java Code Generators
Top comments (0)