Writing a REST Service with Spring Boot: Difference between revisions
Line 89: | Line 89: | ||
=Test the Service= | =Test the Service= | ||
<syntaxhighlight lang='bash'> | |||
./gradlew bootRun | |||
curl http://localhost:8080/status | |||
</syntaxhighlight> | |||
=Build the Container Image= | =Build the Container Image= | ||
=Deploy in Kubernetes with Helm= | =Deploy in Kubernetes with Helm= |
Revision as of 00:27, 7 February 2022
External
Internal
Overview
This article describes assembling a simple REST service with Spring Boot, from scratch. The code can be compiled, executed locally, packaged as an OCI container and then deployed in Kubernetes with Helm.
The code is available under:
More ideas: "asset-uploader" under playground/misc/b23Ts.
Prerequisites
The following tools will need to be installed on the development system:
- Java 11 or newer
- gradle
- IntelliJ IDEA
Initialize the Project with Spring Initializr
The project will be initialized with Spring Initializer, from IntelliJ IDEA. Follow the procedure described here:
Spring Boot Dependencies
- Spring Web.
Programming Model
Create a Resource Representation Class
Create the resource representation for the resource that will be accessed via GET /status
and place it in the playground.smoke.model
package:
package playground.smoke.model;
import java.util.UUID;
public class Status {
private final String id;
private final long timestamp;
public Status() {
this.id = UUID.randomUUID().toString();
this.timestamp = System.currentTimeMillis();
}
public String getId() {
return id;
}
public long getTimestamp() {
return timestamp;
}
}
The JSON representation sent back to the client will be:
{
}
Create a Resource Controller
Create the controller that will handle the requests for the Status
resource and place it in the playground.smoke.controller
:
package playground.smoke.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import playground.smoke.model.Status;
@RestController
public class StatusController {
@GetMapping("/status")
public Status status() {
return new Status();
}
}
Build the JAR
./gradlew build
Test the Service
./gradlew bootRun
curl http://localhost:8080/status