Set up Spring Boot Java app with Maven in VS Code
Tags: java, springboot, vscode, quickstart
Published: 2023-03-11 09:23:25

## Useful Links

1. Lets start with the links to some useful resources:
2. What is [REST](https://www.codecademy.com/article/what-is-rest)?
3. Setting up Run/debug configurations in [IntelliJ](https://www.jetbrains.com/help/idea/run-debug-configuration.html)
4. Setting up [Spring Project]
5. Basic Spring [MVC](https://spring.io/guides/gs/serving-web-content/)
6. (https://www.codecademy.com/paths/create-rest-apis-with-spring-and-java/tracks/spring-apis-web-and-spring-basics/modules/how-spring-works/articles/spring-project-layout-and-running-locally)
7. [Project Lombok](https://projectlombok.org/)
8. [H2 Database](https://www.h2database.com/html/tutorial.html)
9. [Jackson](https://github.com/FasterXML/jackson) – known as "the Java JSON library" or "the best JSON parser for Java"
10. [Error Handling](https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc/)
11. [Securing the Application](https://spring.io/guides/gs/securing-web/)
12. [Spring Boot on AWS](https://aws.amazon.com/blogs/opensource/getting-started-with-spring-boot-on-aws-part-1/)

## 1. Installation

- Install Java and Add `JAVA_HOME` in env
- Install Maven if you have not
- Install [Java Extension Pack](vscode:extension/vscjava.vscode-java-pack)
- Install [Spring Boot Extension Pack](vscode:extension/pivotal.vscode-boot-dev-pack)
- Install [Maven for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven)
- Install [Project Manager for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-dependency)

## 2. Generate starter pack
To generate a starter pack, we can either do it via the [spring initializr](https://start.spring.io/) web page OR we can do it directly on vscode.

### Method 1: Via Spring initializr webpage
- Download a maven java project from [spring initializr](https://start.spring.io/) with **Spring Web** dependency.

### Method 2: Via VSCode [Spring Initializr extension](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-spring-initializr)
-  To generate new Spring Boot projects, type `Ctrl+Shift+P` and search `Spring Initializr`. Select "Spring Initializr - Create a Maven Project", then select the Spring Boot version, etc..

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ji1y74nhfx7ez1clhfo.png)

We can also select the dependencies we need:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahyy03rp5csiszfgxqfw.png)

## 3. Open settings.json VS Code
Add the following:
```json
  "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"
```

## 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](https://spring.io/quickstart) and copy the code:
```java
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](http://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`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x94a39igqs38owqz1gdu.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0im6e2df9r3k30z2f8v.png)

## 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o26ps6z3mq2cdoxd9xrf.png)

## 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](https://marketplace.visualstudio.com/items?itemName=sohibe.java-generate-setters-getters) to help with Getters & Setters autocomplete.

OR, We can also use the [Lombok](https://projectlombok.org/features/GetterSetter) dependencies.
You can annotate any field with @Getter and/or @Setter, to let lombok generate the default getter/setter automatically.
```java
import lombok.Getter;
import lombok.Setter;

public class GetterSetterExample {
  @Getter @Setter private int age = 10;
}
```
If not using Lombok, we will need to write more codes:
```java
public class GetterSetterExample {
  private int age = 10;
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}
```