DEV Community

Cover image for Set up Spring Boot Java app with Maven in VS Code
KenjiGoh
KenjiGoh

Posted on • Edited on

Set up Spring Boot Java app with Maven in VS Code

Useful Links

  1. Lets start with the links to some useful resources:
  2. What is REST?
  3. Setting up Run/debug configurations in IntelliJ
  4. Setting up [Spring Project]
  5. Basic Spring MVC
  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
  8. H2 Database
  9. Jackson – known as "the Java JSON library" or "the best JSON parser for Java"
  10. Error Handling
  11. Securing the Application
  12. Spring Boot on AWS

1. Installation

2. Generate starter pack

To generate a starter pack, we can either do it via the spring initializr web page OR we can do it directly on vscode.

Method 1: Via Spring initializr webpage

Method 2: Via VSCode Spring Initializr extension

  • 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

We can also select the dependencies we need:
Image description

3. 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"


Enter fullscreen mode Exit fullscreen mode

4. mvn install:

If needed, close and Reopen your VS Code after configuring the settings.json



mvn clean install


Enter fullscreen mode Exit fullscreen mode

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);
    }
}


Enter fullscreen mode Exit fullscreen mode

6. Run Springboot application



mvn spring-boot:run


Enter fullscreen mode Exit fullscreen mode

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

Image description

Image description

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

Jar file can run when double-click too. Just make sure to associate javaw.exe with jar file.

Image description

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


Enter fullscreen mode Exit fullscreen mode

11. AutoGenerate Getters & Setters

Install Java Code Generators to help with Getters & Setters autocomplete.

OR, We can also use the Lombok dependencies.
You can annotate any field with @Getter and/or @setter, to let lombok generate the default getter/setter automatically.



import lombok.Getter;
import lombok.Setter;

public class GetterSetterExample {
  @Getter @Setter private int age = 10;
}


Enter fullscreen mode Exit fullscreen mode

If not using Lombok, we will need to write more codes:



public class GetterSetterExample {
  private int age = 10;
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}


Enter fullscreen mode Exit fullscreen mode

Top comments (0)