Welcome to quick and easy CRUD application development with Spring Boot!

For this tutorial, nothing is assumed except some basic Java experience. We’ll show you how simple it is to create compliant and flexible REST services using the incredible frameworks Spring and Spring Boot. We’ll go through the how and why of it all, step-by-step so you can comfortably explore further on your own.

Starting From Scratch with Spring Boot and Maven

In order to understand what is going here it’s worth building up a project from scratch, file-by-file and line-by-line. With Spring Boot and Maven this is quite easily done in 20 minutes, but first let’s quickly talk about Spring.

Spring and Spring Boot

Spring is an application framework built on top of Java. It allows for the building of decoupled systems using dependency injection. In this tutorial we’ll create an app that uses an embedded (i.e. in-memory) database and we’ll do this without using any code. The intended implementation library simply needs to be included in the project. If you haven’t worked with Spring you’ll be amazed at how easily components are swapped in and out.

Spring Boot makes building Spring applications easier by requiring almost no configuration. Things like library versions are automatically resolved, and deployment is a breeze. Basically, it doubles down on Spring’s simplification of Java development.

And finally, Spring Data is Spring’s helper library for data access. It’s primary usage is the Spring Data JPA, which lets you connect to engines conforming to the JPA standard. This is what we will be using to wire up our database.

Creating a CRUD App

With all this Spring magic, creating a consistent and compliant REST web service requires four files, in total.

  • pom.xml
  • Application.java
  • Object.java
  • ObjectRepository.java
  • Incredibly, that’s it. A fully working REST server with CRUD and search operations, deployable with one command. And not only that, each file is no more than a dozen lines long.

    Configure With pom.xml

    Maven uses a pom.xml file to configure builds. It has three sections – general properties, dependencies, and the build itself.

    Most pom.xml files start with a lots of xml details.

    But this isn’t necessary for our purposes. (check out Maven’s Introduction to pom for more details) The minimal pom file you need for this tutorial looks like this:

    This builds fine.
    CRUD app with Spring Boot
    Note: I suppressed the build log with > /dev/null to make the screenshot readable.

    The Parent

    Next, we include a parent:

    The parent tag tells Maven that we’d like to use another pom to encapsulate ours, i.e. inherit various properties and tags. Here we’re inheriting from Spring Boot (note the group org.springframework.boot) something called the Spring Boot Starter Parent. We don’t have to inherit from the starter parent. Mainly it’s there to set things up easily. One of nicest features is that you don’t have to include version numbers of Spring Boot libraries in your pom – it will figure them out for you!

    Spring Boot Starters

    Next we define our dependencies – the libraries we’d like to use. Spring Boot comes with one-stop-shop packages called Starters that include everything you need for a ready-made application. (These are what typically what you see in the Spring Initialzr with all the dependencies you can wire in.) We’re going to be using two – Spring Data Rest and Spring Data JPA.

    Two things to note: First, both packages come from the org.springframework.boot group. And second, we didn’t have to specify version numbers. The Starter Parent does that for us!
    For more details on how this works, check out our Spring Boot Starters tutorial.

    Add a Database

    The last dependency we need is our database. If we tried to build with what we have so far it would fail since it’s looking for something to wire the persistence to.

    The easiest way to get set up is to use one that is embedded, which just means it’s in-memory, so you don’t have to connect it to something like MySQL with a url and login details. (I’ll show you how to do that as well at the end, though.)

    This, again amazingly, is it for setting up our database!

    Dependency Wrapper

    Your dependencies (which really is just a fancy way of telling Maven “please download these JARs into my project’s classpath”) need to be wrapped in a node called dependencies.

    Maven Boot Plugin

    The last thing we’re going to do is include a plugin for Maven. Typically running your project is complicated – you could try a java -jar target/demo-0.0.1-SNAPSHOT.jar but this normally fails because you haven’t set up your classpath.
    Actually, the first thing that happens is you get a manifest error.
    Maven Boot Plugin
    You can fix this using the pom but it’s easier use Spring Boot to fix that for us. Simply add this to the bottom.

    That makes running your app simple: just type mvn spring-boot:run, but not right now! If we do this now it will fail – we need a main class first.
    We’re done with the pom though. Here’s the full example:

    Adding Java to Your pom.xml

    So far all we’ve done is created a single file – pom.xml. Now we need to include some Java.
    The first thing we need is the application entry point. Maven starts look for your code in src/main/java. The application must be in a package, so let’s put it in demo.
    Maven
    Maven demo
    The contents of Application.java are remarkably straight forward.

    If that doesn’t blow you away I don’t know what will. Everything is wired up automatically. We didn’t even have to specify this class in our pom! Spring Boot does that for us by searching for a class that defines a main function in src/main/java.
    We now have a fully working application with just these two files:
    Maven test
    Maven and Tomcat
    As you can see, it has started a Tomcat instance on port 8080. We have a running server with standardized logging and REST functionality!

    Curl

    To see what’s on the server we use curl.
    Curl
    If you don’t know curl, we’ve just called GET on the server and it’s returned JSON. What is returned is a list of what you can do on the server (the _links section). Right now it’s saying there is only one thing available – http://localhost:8080/profile, which is just a reference to ourself.
    Curl JSON

    Adding an Object

    To create something that can be persisted we use two more Java files – the definition of the object itself, and the repository used for storage.
    Adding an Object
    For Person.java we define a standard Java bean but with a few annotations.

    For the repository we simply define an interface that extends CrudRepository, and annotate using @RepositoryRestResource.

    Booyah!

    And that’s it! We now have a fully REST-compliant CRUD server!
    Screenshot from 2016-07-22 15-59-49
    Screenshot from 2016-07-22 16-00-27
    Screenshot from 2016-07-22 16-05-41
    Now when we call GET on the server we get another link: persons (just the name of your class with an s added). To see what persons we have get just curl on the url.
    Screenshot from 2016-07-22 16-07-13
    Now besides the links we see an embedded section which shows the contents of our embedded database, it has one table – persons. And it’s empty.
    Screenshot from 2016-07-22 16-11-17

    If we POST the JSON contents to localhost:8080/persons we get back the person – localhost:8080/persons/1.
    Screenshot from 2016-07-22 16-12-39
    Boom.

    There’s so much more!

    This tutorial should get you set with a basic CRUD application and very few moving parts. From here you could add in features like, switching out your database, using paging results, and writing tests. To see this project in full, clone it on GitHub. Or, to learn more about Spring Boot and adding authentication to your application, check out these resources:

  • 5 Practical Tips for Building Your Spring Boot API
  • OZork Auth — Learn OAuth 2.0 + Spring Boot the Fun Way!
  • A Simple Web App With Spring Boot, Spring Security, and Stormpath — in 15 Minutes