To keep it short: Gradle automates stuff. It is able to automate everything, e.g. building and deploying your project. This can dramatically simplify your workflow! And, aside from that, it helps you with managing your dependencies to third party libraries by downloading them for you. If you have worked previously with build or dependency management tools like Maven or Ant, then Gradle surely sounds familiar.
Later we will integrate the dependency fetching of bower with Gradle, so that you just have to run one command on a clean computer to build and run your application. Trust me, once you are getting used to the comfort of builds with Gradle, you will never want to miss it!

Spring Initializr

Alright, lets start! Open Spring Initializr in a new tab (or window, if you prefer that). The page you just opened helps you setting up a new project by asking you some questions about the libraries you are going to use. On the left side, next to Type, you should select Gradle Project in the drop-down menu. Select Spring Boot Version 1.3.0M4. On the right side, you should check the following dependencies:

Security (Core)

Rest Repositories (Web)

Thymeleaf (Template Engines)

JPA (Data)

H2 (Database)

You may now press Generate Project. Extract the contents of the zip File to a destination of your choice. We are now ready to import the project into our IDE.
If you are using eclipse, install the official Gradle Integration. If you are using IntelliJ, you are ready to go.

Eclipse

Select File -> Import -> Gradle -> Gradle Project -> Next -> (insert the path to your unzipped project) -> Finish.
The fetching of the dependencies will take a while.

IntelliJ

Select Open -> (select your build.gradle in your project directory) -> OK -> Use customizable gradle wrapper -> OK
The fetching of the dependencies will take a while.

Step 2: The minimal REST API with Spring Data Rest

We are almost ready to go. We could run the application now, but we would not see very much. Therefore, let’s make two further improvements:

Deactivate Spring Security (for now)

In the current state would our web application not be accessible, because spring security requires us to authenticate. Lets disable authentication until we can manage user accounts. Open /src/main/resources/application.properties and add:

Add a note entity and a repository

Every application has some kind of data which is created, read, updated and deleted (CRUD). Classes which hold data are often referred to as entities. Spring Data allows us to create so called repositories for our entities. Repositories provide all of the CRUD methods (creating, reading, updating and deleting an entity) in a reusable manner, so that we do not have to write the boilerplate code for interacting with the database. If you have experience with JPA, than you will be surprised how Spring Data accelerates and simplifies the development even further!

Note class

Copy the following contents into src/main/java/demo/domain/note/Note.java

The highlighted line is part of the magic from Spring Data. The CrudRepository<Note, Integer> provides the aforementioned CRUD operations. Of course are we able to further customize it, but for the moment should you enjoy that you only have to write an interface declaration and let the framework handle the concrete implementation for you. By the way, the generic arguments <Note, Integer> are the entity (in our case a Note) and the type of the primary key for this entity. You may want to have a look at the JavaDoc of the CrudRepository for further information.

Folder and package structure

As you now have created the notes entity and a corresponding repository, let us take a short break and reflect on how we have arranged it.

src/main/java/demo
^ ^ ^ ^
| | | | the main project folder.
| | |
| | | java contains - well - Java source files. You might ask, what else could here be?
| | Groovy for example, an alternative language for the JVM.
| |
| | main stands in this case for the main source files. On this level you would also have test,
| which will contain the unit tests (in a later post).
|
| The main source directory. Everything in it will be executable.
We have a /res directory on this level too. res stands for resources
and contains other files related to the project, e.g. HTML and JavaScript
Inside the main project folder the structure looks like this
demo (main project folder)
└───domain (contains everything related to our entities and domain objects)
├───notes (contains everything related to notes, e.g. the entity and the repository)
└───categories (not there yet, but would contain anything related to categories)

ALPS (Application-Level Profile Semantics)

The answer was generated by Spring Data Rest. Lets get in detail with the design of the answer, as it plays an essential role and understanding it is very important. _links is always a pointer to further resources, often including the current displayed resource. profile is a link to ALPS (Application-Level Profile Semantics). This is especially useful for consumers of your REST API, as ALPS describe the semantics of it. It also contains the structure of the REST accessible entities.

Notes REST representation

Further Information:
Spring has out of the box support for a web user interface for managing a h2 database.
You can now add spring.h2.console.enabled=true in a new line to your application.properties. If you restart your application now, you are able to access http://localhost:8080/h2-console/

Log in with the following data:

Key

Value

JDBC URL

jdbc:h2:mem:testdb

User name

sa

Password

blank

After a click on connect you have just opened the web console. In it, you can explore the created database scheme and issue queries. Try

The response contains two elements. _links and _embedded. The first one should be familiar now. It contains in this case the link to the current resource - the so called self-link. _embedded contains notes, and notes is an empty array. Maybe you have guessed it! It is an empty array, because we do not have any information stored yet. Lets add some sample data. Create the file src/main/resources/data.sql with the following content.

Woah! It works! But why was data.sql automatically executed? Answer: It is Spring Boot magic.
Let us examine the response further. notes is now an array consisting of two note entities. Each of them has a title-, a content- and a _links-property. title and content are what you expect them to be. _links contains two links on each note entity. Actually, you can ignore the "note"-link, it is the same as the self-link and not mentioned in the Spring Data Rest docs.
The self-link plays, as mentioned before, an important role. If you want to update e.g. the title of the first note entity, you have to issue HTTP-Request to the self link of the note.

Creating, Updating and deleting: PATCH, PUT, POST, DELETE?

You have now a REST API for managing your notes. It will be consumed by your AngularJS application. To create and manipulate your notes, you will need to use the HTTP-Verbs POST, PUT, PATCH and DELETE.

If you want to try the following requests, depending on your browser, you may install an extension for easier crafting of custom requests.

For configuration of the extensions you can use the following screenshots. Please note, that you shall always set the Content-Type-Header to application/json.

POST - create new entities

We have manually created some notes via an SQL script, which is ran on application startup. You probably want to create new notes in your application dynamically. All you have to do is issue a POST-Request to http://localhost:8080/notes with the following content:

{"title":"enter your title here","content":"enter your content here"}

Chrome

Firefox

Modifying Entities: PUT/PATCH

If you want to update an existing note, you have two ways for doing so. You either can replace the complete entity with whole new data, or you can partially modify some properties.

Summary

You have learned how to create the project from scratch, using Gradle and Spring Initializr. You have created a Note entity and the corresponding repository. You played around with the REST API to get a feeling for how to use it to retrieve, create and manipulate notes.