Quarkus - Generating JAX-RS resources with Panache

A lot of web applications are monotonous CRUD applications with REST APIs that are tedious to write. To streamline this task, REST Data with Panache extension can generate the basic CRUD endpoints for your entities and repositories.

While this extension is still experimental and provides a limited feature set, we hope to get an early feedback for it. Currently this extension supports Hibernate ORM with Panache and can generate CRUD resources that work with application/json and application/hal+json content.

This technology is considered experimental.

In experimental mode, early feedback is requested to mature the idea. There is no guarantee of stability nor long term presence in the platform until the solution matures. Feedback is welcome on our mailing list or as issues in our GitHub issue tracker.

For a full list of possible extension statuses, check our FAQ entry.

Setting up REST Data with Panache

To begin with:

  • Add the required dependencies to your pom.xml

    • Hibernate ORM REST Data with Panache extension (quarkus-hibernate-orm-rest-data-panache)

    • A JDBC driver extension (quarkus-jdbc-postgresql, quarkus-jdbc-h2, quarkus-jdbc-mariadb, …​)

    • One of the RESTEasy JSON serialization extensions (quarkus-resteasy-jackson or quarkus-resteasy-jsonb)

  1. <dependencies>
  2. <dependency>
  3. <groupId>io.quarkus</groupId>
  4. <artifactId>quarkus-hibernate-orm-rest-data-panache</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.quarkus</groupId>
  8. <artifactId>quarkus-jdbc-postgresql</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>io.quarkus</groupId>
  12. <artifactId>quarkus-resteasy-jackson</artifactId>
  13. </dependency>
  14. </dependencies>
  • Implement the Panache entities and/or repositories as explained in the Hibernate ORM with Panache guide.

  • Define the interfaces for generation as explained in the sections below.

Generating resources

REST Data with Panache generates JAX-RS resources based on the interfaces available in your application. For each entity and repository that you want to generate, provide an interface that extends either PanacheEntityResource or PanacheRepositoryResource interface. Do not implement these interfaces and don’t provide custom methods because they will be ignored. You can, however, override the methods from the extended interface in order to customize them (see the section at the end).

PanacheEntityResource

If your application has an entity (e.g. Person) that extends either PanacheEntity or PanacheEntityBase class, you could instruct REST Data with Panache to generate its JAX-RS resource with the following interface:

  1. public interface PeopleResource extends PanacheEntityResource<Person, Long> {
  2. }

PanacheRepositoryResource

If your application has a simple entity (e.g. Person) and a repository (e.g. PersonRepository) that implements either PanacheRepository or PanacheRepositoryBase interface, you could instruct REST Data with Panache to generate its JAX-RS resource with the following interface:

  1. public interface PeopleResource extends PanacheRepositoryResource<PersonRepository, Person, Long> {
  2. }

The generated resource

The generated resources will be functionally equivalent for both entities and repositories. The only difference being the particular data access pattern in use.

If you have defined one of the PeopleResource interfaces mentioned above, REST Data with Panache will generate a JAX-RS resource similar to this:

  1. @Path("/people") // Default path is a hyphenated lowercase resource name without a suffix of `resource` or `controller`.
  2. public class PeopleResourceImpl implements PeopleResource { // The actual class name is going to be unique
  3. @GET
  4. @Produces("application/json")
  5. @Path("{id}")
  6. public Person get(@PathParam("id") Long id){
  7. // ...
  8. }
  9. @GET
  10. @Produces("application/json")
  11. public Response list(){
  12. // ...
  13. }
  14. @Transactional
  15. @POST
  16. @Consumes("application/json")
  17. @Produces("application/json")
  18. public Response add(Person entity) {
  19. // ..
  20. }
  21. @Transactional
  22. @PUT
  23. @Consumes("application/json")
  24. @Produces("application/json")
  25. @Path("{id}")
  26. public Response update(@PathParam("id") Long id, Person person) {
  27. // ..
  28. }
  29. @Transactional
  30. @DELETE
  31. @Path("{id}")
  32. public void delete(@PathParam("id") Long id) {
  33. // ..
  34. }
  35. }

Resource customisation

REST Data with Panache provides a @ResourceProperties and @MethodProperties annotations that can be used to customize certain features of the resource. It can be used in your resource interface:

  1. @ResourceProperties(hal = true, path = "my-people")
  2. public interface PeopleResource extends PanacheEntityResource<Person, Long> {
  3. @MethodProperties(path = "all")
  4. Response list();
  5. @MethodProperties(exposed = false)
  6. void delete(Long id);
  7. }

Available options

@ResourceProperties

  • hal - in addition to the standard application/json responses, generates additional methods that can return application/hal+json responses if requested via an Accept header. Default is false.

  • path - resource base path. Default path is a hyphenated lowercase resource name without a suffix of resource or controller.

  • paged - whether collection responses should be paged or not. First, last, previous and next page URIs are included in the response headers if they exist. Request page index and size are taken from the page and size query parameters that default to 0 and 20 respectively. Default is true.

@MethodProperties

  • exposed - does not expose a particular HTTP verb when set to false. Default is true.

  • path - operation path (this is appended to the resource base path). Default is an empty string.

Response body examples

As mentioned above REST Data with Panache supports the application/json and application/hal+json response content types. Here are a couple of examples of how a response body would look like for the get and list operations assuming there are five Person records in a database.

GET /people/1

Accept: application/json

  1. {
  2. "id": 1,
  3. "name": "John Johnson",
  4. "birth": "1988-01-10"
  5. }

Accept: application/hal+json

  1. {
  2. "id": 1,
  3. "name": "John Johnson",
  4. "birth": "1988-01-10",
  5. "_links": {
  6. "self": {
  7. "href": "http://example.com/people/1"
  8. },
  9. "remove": {
  10. "href": "http://example.com/people/1"
  11. },
  12. "update": {
  13. "href": "http://example.com/people/1"
  14. },
  15. "add": {
  16. "href": "http://example.com/people"
  17. },
  18. "list": {
  19. "href": "http://example.com/people"
  20. }
  21. }
  22. }

GET /people?page=0&size=2

Accept: application/json

  1. [
  2. {
  3. "id": 1,
  4. "name": "John Johnson",
  5. "birth": "1988-01-10"
  6. },
  7. {
  8. "id": 2,
  9. "name": "Peter Peterson",
  10. "birth": "1986-11-20"
  11. }
  12. ]

Accept: application/hal+json

  1. {
  2. "_embedded": [
  3. {
  4. "id": 1,
  5. "name": "John Johnson",
  6. "birth": "1988-01-10",
  7. "_links": {
  8. "self": {
  9. "href": "http://example.com/people/1"
  10. },
  11. "remove": {
  12. "href": "http://example.com/people/1"
  13. },
  14. "update": {
  15. "href": "http://example.com/people/1"
  16. },
  17. "add": {
  18. "href": "http://example.com/people"
  19. },
  20. "list": {
  21. "href": "http://example.com/people"
  22. }
  23. }
  24. },
  25. {
  26. "id": 2,
  27. "name": "Peter Peterson",
  28. "birth": "1986-11-20",
  29. "_links": {
  30. "self": {
  31. "href": "http://example.com/people/2"
  32. },
  33. "remove": {
  34. "href": "http://example.com/people/2"
  35. },
  36. "update": {
  37. "href": "http://example.com/people/2"
  38. },
  39. "add": {
  40. "href": "http://example.com/people"
  41. },
  42. "list": {
  43. "href": "http://example.com/people"
  44. }
  45. }
  46. }
  47. ],
  48. "_links": {
  49. "add": {
  50. "href": "http://example.com/people"
  51. },
  52. "list": {
  53. "href": "http://example.com/people"
  54. },
  55. "first": {
  56. "href": "http://example.com/people?page=0&size=2"
  57. },
  58. "last": {
  59. "href": "http://example.com/people?page=2&size=2"
  60. },
  61. "next": {
  62. "href": "http://example.com/people?page=1&size=2"
  63. }
  64. }
  65. }

Both responses would also contain these headers:

A previous link header (and hal link) would not be included, because the previous page does not exist.