RESTful JPA One-liners With Rapidoid

DZone 's Guide to

RESTful JPA One-liners With Rapidoid

Learn how to implement RESTful services with simple JPA CRUD functionality using the web framework Rapidoid in this tutorial.

· Integration Zone ·
Free Resource

Let's see how easy it is to implement RESTful services with simple JPA CRUD functionality with Rapidoid, the almighty web framework.

Bootstrapping JPA and Implementing RESTful Services

import org.rapidoid.jpa.JPA;
import org.rapidoid.setup.App;
import org.rapidoid.setup.On;

public class JPADemo {
 public static void main(String[] args) {
  App.run(args).jpa(); // bootstrap JPA

  On.get("/books").json(() -> JPA.of(Book.class).all());

  On.get("/books/{id}").json((Long id) -> JPA.get(Book.class, id));

  On.post("/books").json((Book b) -> JPA.insert(b));

  On.put("/books/{id}").json((Book b) -> JPA.update(b));

Of course, we also need some JPA entity (which is over-simplified for this demonstration):

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class Book {
 public Long id;

 public String title;

 public int year;

For a quick start, the rapidoid-quick module should be used. It contains the Rapidoid HTTP server and web framework, Hibernate and other goodies:


That's it! The application is ready to run!

Testing the API

Let's quickly test the API, by sending some HTTP requests and printing the responses.

First, let's insert a new book:

Map < String, ? > book1 = U.map("title", "Java Book", "year", 2016);

Self.post("/books").data(book1).print(); // {"id":1,"title":"Java Book","year":2016}

Then, let's update the same book:

Map < String, ? > java9book = U.map("title", "Java 9 Book", "year", 2017);

Self.put("/books/1").data(java9book).print(); // {"id":1,"title":"Java 9 Book","year":2017}

And the book is still there:

Self.get("/books").print(); // [{"id":1,"title":"Java 9 Book","year":2017}]

Under the Hood

As soon as the application is started, Rapidoid will start up in several important steps:

  • Scan the classpath for JPA entities,

  • Bootstrap Hibernate and the built-in JPA service,

  • Register the JPA entities with Hibernate,

  • Register the lambda handlers for the specified REST endpoints,

  • Start the built-in high-performance HTTP server.

Rapidoid's built-in JPA service is doing all the heavy lifting when working with JPA:

  • Providing high-level API for CRUD JPA operations and more,

  • Creating and managing EntityManager instances,

  • Managing JPA transactions.


Rapidoid makes working with JPA super easy. With a few lines of code, we were able to implement few JPA-based RESTful services.

Some important and related aspects haven't been covered: bean validation, integration tests, transaction management, etc. They remain as topics for another article...

integration, java, java 8, jpa, one-liner, rest, rest services

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}