Jackson @JsonIgnore and @JsonProperty annotation with Example





Hey guys in this post, we will discuss Jackson @JsonIgnore and @JsonProperty annotation with Example.

Overview


Jackson is a very popular and efficient java based library to serialize or map java objects to JSON and vice versa.

The Jackson annotation @JsonIgnore is used to tell Jackson to ignore a certain property (field) of a Java object. The property is ignored both when reading JSON into Java objects, and when writing Java objects into JSON.

public class Employee {
	
	@JsonIgnore
	private Long age;
	
	//setters and getters
	
}

By adding @JsonIgnore annotation on the field, Jackson will ignore this field.

The Jackson annotation @JsonProperty annotation to indicate that the property name in JSON.

public class Employee {
	
	@JsonProperty("full_name")
	private String name;
	
	//setters and getters
}

By adding @JsonProperty on the name field, Jackson will consider the full_name instead of name field.

Example on @JsonIgnore and @JsonProperty annotation


The best way to understand @JsonIgonreand @JsonProperty is by looking at the example

Create spring boot project


There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add maven dependencies


Open pom.xml and add the following dependencies –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur</groupId>
	<artifactId>restcontrollerannotation</artifactId>
	<version>v1</version>
	<name>restcontrollerannotation</name>
	<description>Spring boot restcontroller annotation demo</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container.




spring-boot-devtools dependency for automatic reloads or live reload of applications.

Create an entity class


Create Employee.java inside the in.bushansirgur.springboot.entity package and add the following content

package in.bushansirgur.springboot.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Employee {
	
	@JsonProperty("full_name")
	private String name;
	
	@JsonIgnore
	private Long age;
	
	private String location;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getAge() {
		return age;
	}

	public void setAge(Long age) {
		this.age = age;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}
	
}

Here we are adding @JsonIgnore annotation to the age field and @JsonProperty to the name field. Now when we are sending json payload in the request body, we need to use the property full_name instead of name property and when we get the HTTP response, we don’t get the age property in the response body.

Create a Rest controller


Create HomeController.java inside the in.bushansirgur.springboot.controller package and add the following content

package in.bushansirgur.springboot.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import in.bushansirgur.springboot.model.Employee;

@RestController
public class HomeController {
	
	@PostMapping("/employees")
	public Employee saveEmployee (@RequestBody Employee employee) {
		return employee;
	}
}

We have annotated the class with @RestController annotation, this indicates that the class serves the role of controller and also returns JSON response from all the methods.

Run the app


Run the application using the below maven command –

mvn spring-boot:run

Open the browser and navigate to the URL http://localhost:8080/employees
Capture-1



About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *