Writing a REST Service with Spring Boot: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(34 intermediate revisions by the same user not shown)
Line 2: Line 2:
* https://spring.io/guides/gs/rest-service/
* https://spring.io/guides/gs/rest-service/
* https://spring.io/guides/tutorials/rest/
* https://spring.io/guides/tutorials/rest/
* https://www.baeldung.com/spring-boot-testing


=Internal=
=Internal=
Line 8: Line 7:


=Overview=
=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.


"asset-uploader" under playground/misc/b23Ts.
The code is available under: {{External|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:
 
* Java 11 or newer
* [[gradle]]
* [[IntelliJ|IntelliJ IDEA]]
 
=Initialize the Project with Spring Initializr=
 
The project will be initialized with Spring Initializer, from IntelliJ IDEA. Follow the procedure described here:
 
{{Internal|IntelliJ_IDEA_Plugin_for_Spring_Boot#Create_a_New_Spring_Boot_Project_with_Spring_Initializr|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 <code>GET /status</code> and place it in the <code>playground.smoke.model</code> package:
<syntaxhighlight lang='java'>
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;
    }
}
</syntaxhighlight>
 
The JSON representation sent back to the client will be:
<syntaxhighlight lang='json'>
{
  "id":"716e3018-6811-467f-bf62-767015d24b76",
  "timestamp":1644193611071
}
</syntaxhighlight>
 
==Create a Resource Controller==
Create the controller that will handle the requests for the <code>[[#Create_a_Resource_Representation_Class|Status]]</code> resource and place it in the <code>playground.smoke.controller</code>:
 
<syntaxhighlight lang='java'>
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();
    }
}
</syntaxhighlight>
 
=Build the JAR=
<syntaxhighlight lang='bash'>
./gradlew build
</syntaxhighlight>
 
=Test the Service running Locally=
<syntaxhighlight lang='bash'>
./gradlew bootRun
 
curl -s http://localhost:8080/status | jq
</syntaxhighlight>
 
=Build the Container Image=
{{Internal|Creating_Container_Images_for_Spring_Boot_Applications#Overview|Creating Container Images for Spring Boot Applications}}
 
=Deploy in Kubernetes with Helm=
<syntaxhighlight lang='bash'>
helm -n test install smoke ./src/main/helm/smoke
</syntaxhighlight>
 
The Helm chart is available [https://github.com/ovidiuf/playground/tree/master/spring/smoke/src/main/helm/smoke here].
 
=Test the Service running in Kubernetes=
<syntaxhighlight lang='bash'>
curl -s http://localhost/status | jq
</syntaxhighlight>

Latest revision as of 01:40, 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;
    }
}

The JSON representation sent back to the client will be:

{
  "id":"716e3018-6811-467f-bf62-767015d24b76",
  "timestamp":1644193611071
}

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 running Locally

./gradlew bootRun

curl -s http://localhost:8080/status | jq

Build the Container Image

Creating Container Images for Spring Boot Applications

Deploy in Kubernetes with Helm

helm -n test install smoke ./src/main/helm/smoke

The Helm chart is available here.

Test the Service running in Kubernetes

curl -s http://localhost/status | jq