Spring Starters
Spring Boot is the fastest way to start building Spring-based applications. You can start generating a project from Spring Initializr: https://start.spring.io You can also start the project using Spring CLI:
spring init \
--boot-version=4.0.0 \
--dependencies=web \
--group-id=com.example \
--artifact-id=demoapp \
--name=DemoApp \
--package-name=com.example \
--description="Sample Spring Boot Demo" \
--build=maven \
demoapp
Generate Project Structure
demoapp/
├─ mvnw
├─ pom.xml
├─ src/main/java/com/example/DemoApp.java
├─ src/main/resources/application.properties
└─ src/test/java/com/example/DemoAppTests.java
Pom
The application build configuration is defined in pom.xml
<?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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>quick_start</artifactId>
<version>0.0.1</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Demo Application
/**
* SPRING BOOT - QUICK START
* *
* Spring Boot sits on top of the Spring Framework removes much of the boilerplate,
* letting us build production-ready application quickly.
*
* Spring Boot is the fastest way to start building Spring-based applications.
* You can start generating a project from Spring Initializr:
* https://start.spring.io
*
* POM:
* *
* The application build configuration is defined in pom.xml
* Build and run the application using Maven.
* Test the application with embedded Apache Tomcat server.
*
* mvn package
* mvn spring-boot:run
* curl http://localhost:8080/hello OR
*
* java -jar .\target\demo-0.0.1.jar
* curl http://localhost:8080/hello
* # Hello World
*/
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApp {
public static void main(String[] args) {
SpringApplication.run(DemoApp.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Hello World";
}
}
Starter
A starter in Spring Boot is a preconfigured set of dependencies. Starters remove the need to manually manage dozen of individual dependencies. This is the starter used to build REST APIs and web applications with Spring Boot.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
This is the most common starter in Spring Boot because it enables:
REST controllers (@RestController)
JSON serialization/deserialization
Embedded web server (Tomcat)
Spring MVC infrastructure
Started example
/**
* STARTERS - SPRING BOOT
* *
* A starter in Spring Boot is a preconfigured set of dependencies.
* Starters remove the need to manually manage dozen of individual dependencies.
*
* You include one dependency and automatically get:
* - All required libraries
* - Matching, compatible versions
* - Auto-configuration that works
*
* Starter used to build REST APIs and web application:
*
* <dependency>
* <groupId>org.springframework.boot</groupId>
* <artifactId>spring-boot-starter-web</artifactId>
* </dependency>
*
* This enables:
* - REST controllers (@RestController)
* - JSON (serialization/deserialization)
* - Embeded web server (Tomcat)
* - Spring MVC infrastructure
*
* This code show how Spring Boot automatically enables JSON
* serialization using Jackson - without adding Jackson manually.
*
* mvn spring-boot:run
* curl http://localhost:8080/user
* {"name":"Ana","age":30}
*/
package com.example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StarterExample {
@GetMapping("/user")
public User getUser() {
return new User("Ana", 30);
}
}
/**
* Simple data model (POJO - Plain Old Java Object).
* Jackson, provided by starter, automatically serializes this class
* to JSON when returned from a controller.
*/
class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
}