REST Assured ObjectMapper Serialization with JSON and XML

Often when you are using REST Assured, you will want to serialize a POJO (Plain old Java Object) and send that in your API call. Or you might want to take the API response and de-serialize the body to a POJO. This blog post on REST Assured Serialization will help you do that!

I got a lot of inspiration for this blog post from a similar one published by Bas Dijkstra called (De)serializing POJOs in REST Assured. Bas has a lot of great REST Assured posts you should definitely check out!

Setup

One important step with REST Assured Serialization is to add the Jackson serialization to your classpath. This step isn’t obvious in most REST Assured documentation.

To do it, simply add the following dependency to your Maven or Gradle build script:

compile 'com.fasterxml.jackson.core:jackson-databind:2.8.6'

Now you are ready to start some REST Assured Serialization!

Test Class / Code

In this blog post I will be using the Video Game Database API Application that I am developing for my upcoming REST Assured course. I will post the appropriate code snippets here too so that you can follow along.

In this post we will serialize / de-serialize a VideoGame object. So our VideoGame class looks something like this:

Now that we have our POJO class we can start writing some REST Assured Serialization tests!

JSON Serialization

Let’s start with REST Assured Serialization with JSON. I want to send a POST request to my VideoGame API that will add a new Video Game to the database. I will send a POJO of the video game in the request. This is what the code looks like in the test class:

So we instantiated a new Video Game object called myVideoGame. And we sent that came in the body of the POST request to the API. We then asserted that we got a successful response. Using a proxy, I can see that this is what the request body looked like:

REST Assured Serialization with Jackson handled all the serialization work for us. Great!

JSON De-Serialization

Now let’s write another test that calls a GET request to the API to return the video game we just created. We are going to use Jackson REST Assured Serialization to convert the response straight into a POJO. Here’s the code:

Again, REST Assured Serialization with Jackson did all the work for us.

Conclusion

Using the power of REST Assured Serialization is a useful tool to have in your REST Assured vocabulary. You can use any serialization software that you want (not just Jackson), and even your own custom serialize. Find out more at the REST Assured documentation for Object Mapping

Walkthroughs of REST Assured Serialization will be covered in my upcoming video tutorial series on REST Assured.