Creating a springboot CRUD api Using the Jackson ObjectMapper

Ivan Lifanica
Javarevisited
Published in
3 min readJun 5, 2021

--

Overview

This guide aims on providing an understanding on how to write/read JSON to and from basic POJOs(Plain Old Java Objects) as well as other functionalities for performing conversions.

To learn more about the Object Mapper, here is a good place to start.

We’ll be using this simple crud project as a base for this tutorial. It’s a CRUD API where you can manage products stored in a MySql database;

In future posts, we’ll be using this project as a base adding new functionalities.

In this tutorial, we will cover the following topics:

1- Building our Products class and attributes and use the JPA repository implementations;

2- Creating CRUD (Create, Read, Update, Delete) endpoints for our Products class with a respective Controller(referred as Resource in the project) and Service using the ObjectMapper to serialize and deserialize JSON data;

3- Creating a custom service to handle the requests and responses made in our application;

4- Creating a custom (DTO) annotation for the customer service in no.3 to be applied to the annotated classes;

Getting Started

We’ll first need to create a new project with Spring Initializr.

Make sure to select:

  • jdk 8+;
  • Maven as your build tool

On your preferred IDE add the following dependencies to your POM.xml

d

1- Creating Our ProductEtity and ProductDTO classes:

DTO (Data Transfer Object) is usually an instance of a POCO (plain old CLR object) class used as a container to encapsulate data and pass it from one layer of the application to another.

In our case, we’ll be using DTO since we don’t want to use all the properties from the ProductEntity when we make our requests.

Let’s Use class-level annotation @JasonIgnoreProperties, It will ignore every property we haven’t defined in our POJO(Plain Old Java Object), Very useful when we are just looking for a couple of properties in the JSON and don’t want to write the while mapping.

Since we don’t want to define all the Entity properties(only name and description) in our requests let’s create the DTO:

Let’s annotate our class with our custom @DTO annotation, so the requestHandle service we will be creating can be applied whenever we consume(deserealize) or generate(serealize) data.

2- Creating CRUD operations

Let’s now create an interface that will have all the default crud implementations for every future Entity. Let’s extend the JpaRepository to benefit from most of the methods already implemented in the framework:

Let’s now define our API endpoints:

Note that in the create method we receive ProductEntity as our RequestBody(JSON) and let the Jackson.ObjectMapper deserializes it into our POJO.

now we make the crud implementation using the JpaRepository:

3- Creating a custom service to handle the requests and responses made in our application

Let’s create our own service to process Incoming/outcoming JSON data:

Let’s now make an interceptor and add validation for every request.

4- Creating custom annotations

Now let’s create our custom annotations so the services can be applied to the annotated classes:

This annotation will make sure the provided objects will recieve the treatment we defined in our DTOHandler.java and ResponseDTO.java classes.

Let’s test our API with POSTMAN

5. Conclusion

In this tutorial, we have engaged in a quick way to build a spring boot API with CRUD operations using the ObjectMapper to deserialize and serialize JSON into POJOs and the other way around.

Source code

Full project available on this repo.

Thanks for reading! Hope this will help you in any way!! Feel free to leave any comment.

--

--

Ivan Lifanica
Javarevisited

Super knuckle kanuckle bill buckle banana truffle.