REST APP with Spring Boot

 
/**
 * SPRING BOOT - REST APPLICATION DEMO
 * -----------------------------------
 * This example demostrates a minimal Spring Boot application
 * built with Maven.
 * 
 * WHAT SPRING BOOT PROVIDES:
 * --------------------------
 *  - Auto-configuration (no XML)
 *  - Embedded web server (Tomcat by default)
 *  - Executable JAR out of the box
 *  - Sensible defaults for web applications
 * 
 * WHAT THIS APPLICATION DOES:
 * ---------------------------
 *  - Starts the embedded HTTP server
 *  - Exposes two REST endpoints:
 *      GET /       - "Welcome"
 *      GET /hello  - "Hello World"
 * 
 * HOW IT IS RUN:
 * --------------
 *  - mvn spring-boot:run
 *  - OR: java -jar target/app.jar
 * 
 * TEST THE APPLICATION:
 * ---------------------
 * If the port 8080 is already in use, create a properties file:
 * resources/application.properties
 *   - server.port=8081
 * 
 * curl http://localhost:8081
 * curl http://localhost:8081/hello
 */

package demo;

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          // Enable auto-configuration + component scanning
@RestController                 // Marks this class as a REST controller
public class RestAppDemo {

    public static void main(String[] args) {
        SpringApplication.run(RestAppDemo.class, args); 
    }

    @GetMapping("/")
    public String welcome() {
        return "Welcome";
    }

    @GetMapping("/hello")
    public String hello() {
        return "Hello World";
    }

}

Pom

 
<?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>

    <!-- Spring Boot manages dependency versions-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>demo</groupId>
    <artifactId>restAppDemo</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- Spring Boot Web (REST + embedded Tomcat) -->
        <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>
            <!-- Creates an executable JAR automatically -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Build and Run

 
mvn spring-boot:run




References: