Spring Boot 2.6.2 on Raspberry Pi 4

·

4 min read

The goal of this howto is to get a simple Spring Boot application running on a Raspberry Pi 4.

Install 64-bit OS

Installing RaspiOS Bullseye on the RPi3 or RPi4 is straight forward. I recommend using the Raspberry Pi Imager.

After getting Bullseye installed, this is my RPi4.

~$ lsb_release -d
Description:    Debian GNU/Linux 11 (bullseye)
~$ uname -a
Linux raspberrypi 5.10.63-v8+ #1459 SMP PREEMPT Wed Oct 6 16:42:49 BST 2021 aarch64 GNU/Linux
~$

Install SDKMAN

Install SDKMAN to install and configure a JDK.

~$ sudo apt -q install zip
~$ curl -s "https://get.sdkman.io" | bash
~$ source "$HOME/.sdkman/bin/sdkman-init.sh"
~$ sdk version
~$ ==== BROADCAST =================================================================
* 2022-01-06: connor 1.2.1 available on SDKMAN! https://github.com/helpermethod/connor/releases/tag/v1.2.1
* 2022-01-04: micronaut 3.2.4 available on SDKMAN!
* 2022-01-02: jbang 0.86.0 available on SDKMAN! https://github.com/jbangdev/jbang/releases/tag/v0.86.0
================================================================================

SDKMAN 5.13.1
~$

Install a Java JDK

Install the GraalVM for Java 17, using SDKMAN.

~$ sdk install java 21.3.0.r17-grl
Downloading: java 21.3.0.r17-grl

In progress...

######################################### 100.0%

Repackaging Java 21.3.0.r17-grl...

Done repackaging...

Installing: java 21.3.0.r17-grl
Done installing!


Setting java 21.3.0.r17-grl as default.
~$

This took several minutes to complete on RPi 4

Confirm that java is the correct version.

~$ java -version
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment GraalVM CE 21.3.0 (build 17.0.1+12-jvmci-21.3-b05)
OpenJDK 64-Bit Server VM GraalVM CE 21.3.0 (build 17.0.1+12-jvmci-21.3-b05, mixed mode, sharing)
~$

Create simple Spring Boot application

Use the Spring Initializer web API to create a simple example.

The parameters below:

  • name the artifact
  • choose Java 17
  • include the "web" and "actuator" dependencies
  • use latest version of Spring Boot by default
~$ mkdir demo
~$ cd demo
~/demo $ curl https://start.spring.io/starter.tgz -d groupId=dev.dashaun -d artifactId=spring-pi -d name=spring-pi -d packageName=dev.dashaun.spring-pi -d dependencies=web,actuator -d javaVersion=17 | tar -xzf -
~/demo $ ls
HELP.md  mvnw  mvnw.cmd  pom.xml  src
~/demo $

Build the application with Maven

The Spring Initializer includes a recent version of Maven to use for the build.

~/demo $ ./mvnw clean package
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< dev.dashaun:spring-pi >------------------------
[INFO] Building spring-pi 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ spring-pi ---
...
...
...
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  25.854 s
[INFO] Finished at: 2022-01-11T18:36:55Z
[INFO] ------------------------------------------------------------------------
~/demo $

Start the embedded web server

The "web" dependency includes an embedded web server that we can now run.

~/demo $ ./mvnw spring-boot:start
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< dev.dashaun:spring-pi >------------------------
[INFO] Building spring-pi 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.6.2:start (default-cli) @ spring-pi ---
[INFO] Attaching agents: []

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.2)

2022-01-11 18:38:56.137  INFO 13687 --- [           main] d.dashaun.springpi.SpringPiApplication   : Starting SpringPiApplication using Java 17.0.1 on raspberrypi with PID 13687 (/home/pi/fun/demo/target/classes started by pi in /home/pi/fun/demo)
2022-01-11 18:38:56.147  INFO 13687 --- [           main] d.dashaun.springpi.SpringPiApplication   : No active profile set, falling back to default profiles: default
2022-01-11 18:39:01.051  INFO 13687 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-01-11 18:39:01.089  INFO 13687 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-01-11 18:39:01.091  INFO 13687 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-01-11 18:39:01.322  INFO 13687 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-01-11 18:39:01.323  INFO 13687 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 4958 ms
2022-01-11 18:39:03.347  INFO 13687 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'
2022-01-11 18:39:03.481  INFO 13687 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-01-11 18:39:03.534  INFO 13687 --- [           main] d.dashaun.springpi.SpringPiApplication   : Started SpringPiApplication in 8.781 seconds (JVM running for 11.137)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  15.003 s
[INFO] Finished at: 2022-01-11T18:39:03Z
[INFO] ------------------------------------------------------------------------
~/demo $

Our applicaiton is now running on port 8080

Install 'jq'

Install 'jq' first, to format the output.

~/demo $ sudo apt install -yq jq

Call the Spring Boot actuator endpoint

The included the "actuator" dependency provides endpoints to test with.

~/demo $ curl -fsSL localhost:8080/actuator | jq .
{
  "_links": {
    "self": {
      "href": "http://localhost:8080/actuator",
      "templated": false
    },
    "health": {
      "href": "http://localhost:8080/actuator/health",
      "templated": false
    },
    "health-path": {
      "href": "http://localhost:8080/actuator/health/{*path}",
      "templated": true
    }
  }
}
~/demo $ curl -fsSL localhost:8080/actuator/health | jq .
{
  "status": "UP"
}
~/demo $

Summary

For this example we are using a Raspberry Pi 4 to run Spring Boot 2.6.2 using Java 17 on the Raspberry Pi! Even though we are using the GraalVM for this example we are not generating a native application. We will containerize this application into a native image, in a follow up article.