Rest Service

Simple RESTful web service with Spring on default port 8080. Use pom.xml or https://start.spring.io to create a web project.

Pom

 
<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>3.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>current_time</groupId>
    <artifactId>demo</artifactId>
    <version>1.0</version>
    <name>current_time</name>

    <properties>
        <java.version>25</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
</project>

Demo Application

 
package current_time;

import java.time.LocalTime;

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;
import org.springframework.stereotype.Service;

/** 
 * Create a Spring Boot app with a service that returns the current time and a 
 * REST controller that exposes it at GET /time, returning the time as a plain 
 * string in the HTTP response.
 *
 * mvn clean package
 * mvn compile
 * mvn spring-boot:run
 * 
 * curl -s http://localhost:8080/time
 * 16:56:17.090604600
 */

@SpringBootApplication
public class ExampleApp {

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

@RestController
class TimeController {
    private final TimeService timeService;

    public TimeController(TimeService timeService) {
        this.timeService = timeService;
    }

    @GetMapping("/time")
    public String time() {
        return timeService.getCurrentTime();
    }
}

@Service
class TimeService {

    public String getCurrentTime() {
        return LocalTime.now().toString();
    }
}
Use a different port from 8080 (default). src/main/resources/application.properties
 
server.port=9090




References: