Kesha Williams explains mapping requests and responses to Java entities.
- [Instructor] Let's look at how requests are mapped to Java types. We will specifically look at using the Jackson ObjectMapper to use JSON to create Java Objects and vise versa. In JAX-RS, request and response bodies are automatically mapped to and from Java types. The conversion is done by the installed message body reader and message body writer. There are different readers that can handle different content types, and JAX-RS comes with a standard set of readers.
When dealing with complex content types, such as JSON, standard readers don't exist. The most popular JSON framework in Java, Jackson has implemented a JAX-RS provider that has a reader and writer, which handles the serialization and de-serialization automatically behind the scenes. Here's a tip, mapping for XML will work out of the box as long as you add the @XmlRootElement annotation to the model class and enable the MediaType.APPLICATION_XML for the corresponding REST service.
Now, let's discuss the process to add Jackson to our project. I've opened IntelliJ and navigated to the Maven Palm file for the project. So, let's look at that. This is where the Jackson Mapper for JBoss is included. If you look on lines 22 through 27. Now, let's navigate to the JacksonJSONProvider.Java file, and let's look at this. So, this file registers the Jackson Mapper as a provider, as seen here on line 14 with the @Provider annotation.
The Jackson JSON class as seen on line 15, implements the Context Resolver. And configures the ObjectMapper on lines 19 and 20, and returns it on lines 27 through 29. Now, let's look at the ApplicationResource.Java file. Notice here, on line 12, we use the @Consumes annotation, this indicates which My Media type the service will consume or accept from the calling client.
In this case, it's JSON. Now, let's scroll down to line 27, and look at our Add Application method. Now, this method is simply echoing the Application Object that is sent to it, here on line 27. So, Raw JSON will be sent from Postman, and it will automatically be mapped to a Java Application Object. And then, those values will be echoed back to the calling client as JSON.
So, now let's test this out. I have my server up and running. Let's navigate to Postman. So, notice here, the URL we're calling, we're posting to Rest Applications, and we're sending in Raw JSON. So, an Application Object that has an ID, name, and description. So, we'll click Send. And so, JSON is sent to the service. It is then mapped to an Application Object.
And then, here notice, in the response body it's a simple echo back of the data.
- Comparing when to use JAX-WS vs. JAX-RS
- Using JAX-RS annotations
- Accessing REST resources with JAX-RS
- Creating JAX-WS endpoints
- Writing RESTful services using JAX-RS
- Reading, writing, updating, and deleting data via endpoints
- Mapping to entities
- Error handling
- Testing web services
- Securing Java EE web services