Creating a springboot CRUD api Using the Jackson ObjectMapper

Overview

This guide aims on understanding 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 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 atributes 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 serealize and deserealize 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 custom 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

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 our 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 defauld 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 recieve ProductEntity as our RequestBody(JSON) and let the Jackson.ObjectMapper deserealize 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 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 annotations 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 springboot API with CRUD operations using the ObjectMapper to deserialize and serealize 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.

Super knuckle kanuckle bill buckle banana truffle.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store