Spring Boot Setup
To create a Spring Boot web API application, start by creating the directory structure for the Maven project.
/pom.xml
/src/main/java/com/example/Main.java
Within the pom.xml file, set the groupId, artifactId, and version for the project.
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>demoSpring1</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Specify the Java version for the source code and JVM target. This can be done using either properties, as follows:
<properties>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
…or by defining the configuration for the maven-compiler-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>17</source> <!-- maven.compiler.source -->
<target>17</target> <!-- maven.compiler.target -->
</configuration>
</plugin>
Next, add the dependencies within the pom.xml
file, by including the following within the project XML tags (i.e. as a child node of “project”).
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.2</version>
</dependency>
</dependencies>
Any additional dependencies can be added within the dependencies XML tag, such as the following MongoDB driver:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.4.1</version>
</dependency>
The completed pom.xml
file will be as follows:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>demoSpring1</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.4.1</version>
</dependency>
</dependencies>
</project>
The MainClass.java
file contains code for the MVC controller. When developing REST APIs, use the RestController annotation instead of the Controller annotation (Using the Controller instead of RestController, will tell Spring to expect to find a View):
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class MainClass {
public static void main(String args[]) {
SpringApplication.run(MainClass.class, args);
}
@GetMapping("/")
public String index(
@RequestParam(value = "name", defaultValue = "World")
String name
) { return "Hello " + name; }
}
Properties for the application can optionally be defined at /src/main/resources/application.properties
server.error.whitelabel.enabled=false
Execute the application with
mvn compile
mvn exec:java -Dexec.mainClass=com.example.MainClass