Useful Links
- Lets start with the links to some useful resources:
- What is REST?
- Setting up Run/debug configurations in IntelliJ
- Setting up [Spring Project]
- Basic Spring MVC
- (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)
- Project Lombok
- H2 Database
- Jackson – known as "the Java JSON library" or "the best JSON parser for Java"
- Error Handling
- Securing the Application
- Spring Boot on AWS
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. 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
- Download a maven java project from spring initializr with Spring Web dependency.
Method 2: Via VSCode Spring Initializr extension
- To generate new Spring Boot projects, type
Ctrl+Shift+P
and searchSpring Initializr
. Select "Spring Initializr - Create a Maven Project", then select the Spring Boot version, etc..
We can also select the dependencies we need:
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"
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 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;
}
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;
}
}
Top comments (0)