Jackson @JacksonInject Example

By Arvind Rai, March 06, 2018
This page will walk through Jackson @JacksonInject example. @JacksonInject annotation is used for indicating that value of annotated property will be injected. The value of annotated property with @JacksonInject can be injected using Jackson ObjectMapper class. @JacksonInject is helpful in reading JSON. Suppose JSON data has some missing fields and we want a default value to corresponding property of our class, then we can use @JacksonInject annotation. If a property is annotated with @JacksonInject and we have injected value using ObjectMapper then by default if there is no corresponding JSON field, the injected value will be the value of property and if there is corresponding JSON field then the property value will be overridden by corresponding JSON field value. Here on this page we will discuss how to use @JacksonInject with complete example.

Technologies Used

Find the technologies being used in our example.
1. Java 9
2. Jackson 2.9.4
3. Gradle 4.3.1
4. Eclipse Oxygen

Using @JacksonInject

@JacksonInject annotation is used for indicating that value of annotated property will be injected by ObjectMapper class. It has two optional elements, value and useInput and they are as following.

value: Defines logical id of the value to inject.
@JacksonInject("catInput")
private String category; 
Here catInput will be logical id. If value element is not assigned or (it is blank) then type of property is used to inject value.

useInput: It is Boolean and decides whether matching input value is used for annotated property or not. If the value is false, input value will be ignored. If the value is true, input value will override injected value. Default value of useInput is default that has true value.
@JacksonInject(value= "catInput", useInput= OptBoolean.FALSE)
private String category; 

We can use @JacksonInject with @JsonProperty as well.
@JsonProperty("writerBook")	
@JacksonInject("bookInput")
private Book book; 

Inject Value using ObjectMapper

We will inject values using setInjectableValues method of ObjectMapper class. We will discuss two cases, one when value element is assigned and other when it is not assigned. We need to use Jackson InjectableValues.Std to store values to inject. InjectableValues.Std uses simple Map to store values. Find the code snippet to initialize ObjectMapper and InjectableValues.Std.
ObjectMapper mapper = new ObjectMapper();
InjectableValues.Std injectableValues = new InjectableValues.Std(); 
Case-1: value element is assigned.
@JacksonInject("wnameInput")
private String name;
	
@JacksonInject("bookInput")
private Book book; 
The value will be injected using instances of ObjectMapper and InjectableValues.Std as following.
injectableValues.addValue("wnameInput","Mahesh");
injectableValues.addValue("bookInput", new Book("Hibernate Tutorial", "Hibernate"));
		 
mapper.setInjectableValues(injectableValues); 
Jackson will search all @JsonProperty annotated properties with logical id wnameInput and bookInput and inject values in properties respectively as given above.
Case-2: value element is not assigned.
@JacksonInject
private String name;
	
@JacksonInject
private Book book; 
The value will be injected using instances of ObjectMapper and InjectableValues.Std as following.
injectableValues.addValue(String.class,"Mahesh");
injectableValues.addValue(Book.class, new Book("Hibernate Tutorial", "Hibernate"));
		 
mapper.setInjectableValues(injectableValues); 
Jackson will search for the all the properties of the type String and Book and will inject values respectively as given above.

Complete Example

Here we will provide a complete example for @JacksonInject demo. We will create an application in which properties annotated with @JacksonInject will have logical id defined by value element and some properties will have no logical id. Suppose we have created our demo application keeping in mind the following input JSON data for writer.
{
  "writerId" : 110,
  "writerName" : "Mohit",
  "writerBook" : {
    "bookName" : "Learning Java",
    "bookCategory" : "Java"
  }
} 
Now suppose that we are getting input JSON data with missing fields as following.
{
  "writerId" : 110
} 
We can see in the above JSON data that writerName and writerBook fields are missing. These fields will be injected by @JacksonInject annotation. Now find the complete code.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.4'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.4'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.4'
} 
Book.java
package com.concretepage;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Book {
	@JsonProperty("bookName")		
	private String name;
	
	@JsonProperty("bookCategory")	
	@JacksonInject
	private String category;  
	
  	public Book(){}
  	public Book(String name, String category) {
  		this.name = name;
  		this.category = category;
  	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
} 
Writer.java
package com.concretepage;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Writer {
	@JsonProperty("writerId")	
	private Integer id; 
	
	@JsonProperty("writerName")
	@JacksonInject
	private String name;
	
	@JsonProperty("writerBook")	
	@JacksonInject("bookInput")
	private Book book;

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
} 
JSONToObject.java
package com.concretepage;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JSONToObject {
	public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
		 String jsonData = 
		    "{"
			  +"\"writerId\" : 111"
		   +"}";
		 ObjectMapper mapper = new ObjectMapper();
		 InjectableValues.Std injectableValues = new InjectableValues.Std();

		 injectableValues.addValue(String.class,"Default Value");
		 injectableValues.addValue("bookInput", new Book("Hibernate Tutorial", "Hibernate"));
		 
		 mapper.setInjectableValues(injectableValues);
		 
		 Writer writer = mapper.readValue(jsonData, Writer.class);
		 System.out.println(writer.getId()+", "+ writer.getName());
		 Book book = writer.getBook();
		 System.out.println(book.getName()+", "+ book.getCategory());
	}
} 
Output
111, Default Value
Hibernate Tutorial, Hibernate 

References

Annotation Type JacksonInject
Jackson Annotations

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us