Spring boot app on Raspberry Pi

In this tutorial you will learn how to run a Spring boot app on Raspberry Pi, this is not a tutorial on Spring or Spring boot it self. For more information about Spring or Spring boot, see the official documentation. ( https://spring.io/ )

We will create a simple web server which will handle a request and just print out the result.

Requirements

You also need Java installed on your Raspberry Pi. If you have not installed it yet, check out this tutorial. https://learnraspberrypi.com/2019/07/28/java-11-on-raspbery-pi/

Tutorial

Gradle is a build tool which will help you building your application as well as handle external dependencies such as spring boot. In your Java project create a build.gradle file and type the following:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.6.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'hello-world-springboot'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 11
targetCompatibility = 11

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web") 
}

With gradle you can declare dependencies and it will automatically include the library in the build process. With the following lines we declare that we want a dependency to spring-boot and its starter pack ”web”.

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web") 
}

Now that we have the dependency declared we can start coding the application. Create a new java class called HelloWorldApplication.java and type the following:

package org.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Also create a new Java class called HelloWorldController.java

package org.test;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

@GetMapping("/hello/{name}")
public ResponseEntity<String> helloWorld(@PathVariable("name") String name) {
return ResponseEntity.ok().body("Hello " + name);
}
}

This it the application as well as the controller which takes care of the requests from clients.

You can now build the application using Gradle with the following command.

# ./gradlew clean build bootJar

You can now run the application with the command.

# java -jar build/libs/hello-world-springboot-0.1.0.jar

Run your program to Raspberry Pi

Okay so now it runs on your local computer, how to I get it to run on Raspberry Pi. The only thing you need to do is to copy over the jar file to the Raspberry Pi and use the same command to start it.

To copy it over to the Raspberry Pi we will use a handy little tool called scp. Type the following in your terminal:

# scp build/libs/hello-world-springboot-0.1.0.jar pi@raspberrypi.local:/home/pi

The first argument is the file you want to copy, the second is where you would like to copy the file. The format for the second argument is ”user”@”ipaddress”:”path on device”.
e.g. pi@192.168.0.33:/tmp/

When it has been copied to the Raspberry Pi all you need to do is to start it up.

We can now also test the application by using the web browser or a tool like Postman. Visit the website ”{ip address of raspberry}:8080/hello/Stranger”. It will look something like this:

Summary

Congratulations you now know how to run a Spring boot app on Raspberry Pi. You have now explored the very basics of a Spring boot application, the framework is capable of doing so much more! Continue the journey with Spring boot and tell us about it in the comments!

You can find the full source code on github here.

Whats next?

You now have a Spring boot application running on Raspberry Pi, what is your next step? Maybe checking out this tutorial which helps you to measure temperature using Pi4J and Enviro Phat? Or maybe look at this tutorial for Spring boot and Kotlin?