Join Alex Theedom for an in-depth discussion in this video Create the resource entity, part of RESTful Service with JAX-RS 2.0.
- [Voiceover] A resource entity is just a Plain Old Java Object. These POJOs are resources that we want to represent via the REST API to the client and are normally stored in a database. You can imagine that a bookshop application may have many thousands of books and many hundreds of authors in the database. And all this data can be represented with resource entities. And, therefore, resource entity classes can and often do double up as database entities, capable of being persistent in the applications database. Normally, we would use an implementation of the Java Enterprise Edition Java Persistence API, such as Hibernate to manage their persistence.
These objects would be annotated @Entity, and each will be annotated with the appropriate type designate and relationship mapping. However, in order to keep the bookshop application simple, we're not going to implement a persistence layer. So we will not be covering how to configure JPA entities in this course. The only configuration that we need to do to our resource classes is to identify them as being serializable to and from JSON. For this, we use another Java EE API called JAXB. This API is designed to bind JAVA objects to XML, so they can be serialized and deserialized as XML documents.
However, we use JAXB annotations to do the same for JSON, and the only annotation we require is @XmlRootElement. There are many other annotations that can aid serialization and deserialization, but they are out of scope of this course. So once we have created the resources we want to represent via the REST API, we just add this annotation to the class, and we're done. Now we know what we have to do. Let's switch to the IDE and do it. Okay, so I'm gonna start by creating a package for the entities. Well, I want to call it "domain".
So we'll just go up to here, and we're gonna create a package, and we're gonna call it "domain". And now I'm gonna create the Book entity. And, as mentioned before, I'm going to implement the serializable interface. So I implement the serializable interface like so. Now let's add the fields we want the Book resource to have. Now I'm gonna start by adding a field for the book id. This id represents the book's ISBN.
Now let's move on, and I'm going to add a String field for the book's title. Now I'm gonna add a field for the book's description. Now what I want to do is to add a List field for authors. It's possible that our book will have more than one author. We're gonna create the Author class in a moment. Now I'm gonna create another field for the book's price. This is gonna be a Float.
Now I'm gonna continue and create a String field that's gonna store the name of the image of the book's cover. So I'm just gonna call this one "imageFileName". Now let's create another field, and this field is gonna be a link to the author's blog. And the final field I want to create is going to be a Date field, and this is gonna be the published date field. Okay? Now I have created all of the fields, I am going to use the IDE's auto-generation feature to generate the getters and setters.
Your IDE will have the same feature, so just simply go ahead and generate all the getters and setters, the equals() and the hashCode(), and also I'm going to generate a constructor. I'm gonna add in all the fields. Now one thing that's important to remember here is that, if we create a constructor, we must also create a no-argument constructor.
If we don't create a no-argument constructor, the serialization and deserialization will fail. And finally, let's not forget to annotate the class @XmlRootElement like so. Now that I've created my Book resource, I'm going to go ahead and create my Author resource. So, as I did before, I am just going to create my Author class, and also I'm going to implement Serializable. Not forgetting to add the XML root to the class.
So first of all, let's just create a simple String id for the author, so we can uniquely identify the author. Let's create another field for the author's first name and another field for the author's last name. And let's finish by creating a field for the author's blog. As I did before, I'm also going to generate the getters and setters.
I'm also going to generate equals() and hashCode(). And let's generate the toString() method as well. And finally, let's just generate a constructor like so. Of course, as before, we must have a no-argument constructor like so. Doesn't have to have any implementation.
This is just sufficient like this.
Join Alex Theedom as he guides you through a wide range of topics, including how to define resource endpoints, how to add dynamism to your application with hypermedia, how to maintain data integrity with Bean Validation, and how to process messages with the JSON Processing API. Once you have completed this course, you'll be ready to develop your own client and server RESTful APIs.
- Discover the extent of JAX-RS's features
- Use the annotation methodology
- Use the inheritance methodology
- Define a REST contract and API root
- Create the REST resource entity
- Create the REST resource methods
- Understand consumers and producers
- Work with different media types
- Define and work with path parameters
- Build a REST response and handle exceptions
- Implement Bean Validation and manage failures
- Create a RESTful client that consumes the web service
- Use JSON-P API with the REST client
- Implement hypermedia (HATEOAS)