This tutorial will be covering wide range of REST server side topics. Final result will be a simple, lightweight, single jar application works as a REST backend.
Jetty will be used as Web Server.
Jersey will be handling the rest requests.
Data will be stored in memory H2 database.
Hibernate is used to store/retrieve entites to/from H2 database.

You can check the source code of this project on github.

This is the project structure

Project Structure

The main method is in
Rest controllers are in com.bafoly.tutorials.minimalrest.controller package
Entities are under com.bafoly.tutorials.minimalrest.entity package and the EntityManager instance is under com.bafoly.tutorials.minimalrest.jpa

Here’s dependencies in the pom.xml. The full version is here.

main method of

This is basically tying the components together. It starts the web interface of H2 database (Line 5), makes the configuration and starts the Jetty Server (Lines 8-9-12-13 and 28-29) and adds the Jersey Servlet (Line 16) to jetty configuration.
Jersey will scan all the JAX RS annotated classes under the package com.bafoly.tutorials.minimalrest at Line 19.
Jersey will be using Jackson for JSON converter. (Line 21)

You can check here for some examples about Embedded Jetty.

This server will be listenning the port 7070 for http requests.
For Web interface of H2 database, the port is 7071. It can be accessed from http://localhost:7071
You can find further details about H2 database here.

JPA part

This application has two Entities.
Author and Book

The full version of Author is here and Book is here.

Book entity has Genre enum which is persisted to database as String inside Book table.

If you are not familiar with the JPA (Java Persistence API) please take a look at the official documentation. Also it’d be good if you check this page for JPA in standalone applications.
@Entity, @Id, @GeneratedValue, @OneToMany and @ManyToOne annotations are related to JPA. Hibernate checks all the entities in the project and creates tables based on these annotations.


@JsonView is Jackson feature used for generating subset of an entity during serialization/deserialization. For details about it, look here. These views are defined inside the
Let’s say the server will generate a response containing the Author entity
If the controller method is tagged with View.Base.class, then the response JSON will contain Author’s only id, name and lastname variables.
If the controller method is tagged with View.AuthorExtended.class, since the AuthorExtended extends the Base interface, the response will contain the variables tagged with both Base and AuthorExtended. So plus to the id, name and lastname variables, also books will be added to the response. And when the books are added to JSON, Jackson will take the variables which are tagged with View.Base.class or View.AuthorExtended.class from the Book entity.

For JPA configuration there is the persistence.xml under META-INF folder. Full version is here.

minimalRest is a given name for this persistence unit. It will be used during EntityManager creation.
Since this is a standalone application (which means it’ll not be running on JEE Application Server like JBOSS etc) the transactions will be handled locally. To do that transaction-type="RESOURCE_LOCAL" is added. Hibernate is used as provider. The property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" triggers H2 to run in Memory and it will create the database test for the application.
To use JPA in the application, EntityManager must be created. The following code shows that part.

persistence.xml is being read at this point. It checks for minimalRest and reads all the properties of that unit then creates an instance of EntityManager.
This code is in Full version is here.

In the main method of there is generateInitialContent(); which is something like this.

This is how the JPA persists entities to database. Same actions will be used in the REST Controllers.

REST Controllers

There are AuthorController and BookController.
If you are not familiar with the concept, you can check Jersey’s this page for more details. The full versions of AuthorController is here and BookController is here.
A short part from AuthorController is like this.

The controller contains several methods to serve to the HTTP Requests (GET, POST, PUT, DELETE).
The @Path defines how to reach to this controller.
When you execute GET /author, that request will be processed in this method.
@Produces makes this method to generate the response body with specified type, which is JSON for this case. Remember that at the main method of, during the jersey initialization part, for JSON serialization Jackson was set. If it wouldn’t be done there, current method would be throwing exception since it would not be able to convert the Object to JSON.
Other than Jersey’s page, check here for more details about Java’s Rest Service Standard JAX-RS.

Running the app

When you run the app, the database will be loaded with initial data.
You can access to H2 at http://localhost:7071
You’ll be seeing following content.

And of course you can access to these data through REST interface by executing HTTP methods.

I’m using Postman when I want to play with the requests. Postman scripts for this application is here. You can just import and use them. But if you would like to use another tool, here is the api doc
base url: http://localhost:7070
GET /author – list of authors
GET /author/{{id}} – details of author
POST /author – add a new author
PUT /author/{{id}} – update the author
DELETE /author/{{id}} – delete the author

GET /book – list of books
GET /book/{{id}} – details of book
POST /book – add book
POST /author/{{id}}/book – post book to author
PUT /book/{{id}} – update the book
DELETE /book/{{id}} – delete the book

Have fun!