Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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.

Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

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;

@Entity
public class Book {
 @Id
 @GeneratedValue
 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:

<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-quick</artifactId>
<version>5.4.6</version>
</dependency>

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.

Conslusion

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...

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}