Writing a REST Service with Spring Boot: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 34: Line 34:
==Create a Resource Representation Class==
==Create a Resource Representation Class==


Create the resource representation for the resource that will be accessed via <code>GET /status</code>.
Create the resource representation for the resource that will be accessed via <code>GET /status</code> and place it in the <code>playground.smoke.model</code> package:
<syntaxhighlight lang='java'>
package playground.smoke.model;


Place it in the <code>playground.smoke.model</code> package.
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;
    }
}
</syntaxhighlight>


==Create a Resource Controller==
==Create a Resource Controller==

Revision as of 00:13, 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:

https://github.com/ovidiuf/playground/tree/master/spring/smoke

More ideas: "asset-uploader" under playground/misc/b23Ts.

Prerequisites

The following tools will need to be installed on the development system:

Initialize the Project with Spring Initializr

The project will be initialized with Spring Initializer, from IntelliJ IDEA. Follow the procedure described here:

Create a New Spring Boot Project with Spring Initializr

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;
    }
}

Create a Resource Controller

Build the JAR

Test the Service

Build the Container Image

Deploy in Kubernetes with Helm