
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is the use of @JacksonInject annotation using Jackson in Java?n
The Jackson @JacksonInject annotation can be used to inject the values into parsed objects instead of reading those values from the JSON. In order to inject values into a field, we can use the InjectableValues class and need to configure the ObjectMapper class to read both the injected values from the InjectableValues class and the remaining values from the JSON string.
Syntax
@Target(value={ANNOTATION_TYPE,METHOD,FIELD,PARAMETER}) @Retention(value=RUNTIME) public @interface JacksonInject
Example
import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class JacksonInjectTest { public static void main(String args[]) throws IOException { String jsonString = "{\"empName\": \"Raja Ramesh\"}"; InjectableValues injectableValues = new InjectableValues.Std().addValue(int.class, 110); Employee emp = new ObjectMapper().reader(injectableValues).forType(Employee.class).readValue(jsonString); System.out.println(emp); } } // Employee class class Employee { @JacksonInject public int empId = 0; public String empName = "Adithya"; @Override public String toString() { return "Employee{" + "empId=" + empId + ", empName='" + empName + '\'' + '}'; } }
Output
Employee{empId=110, empName='Raja Ramesh'}
- Related Articles
- What is the use of @JsonRawValue annotation using Jackson API in Java?
- When to use @JsonValue annotation using Jackson in Java?\n
- Importance of @JsonView annotation using Jackson in Java?
- Importance of @JsonRootName annotation using Jackson in Java?
- Importance of @JsonIdentityInfo annotation using Jackson in Java?
- Importance of @JsonUnwrapped annotation using Jackson in Java?
- When to use @ConstructorProperties annotation with Jackson in Java?
- Importance of the Jackson @JsonInclude annotation in Java?
- What to use @SerializedName annotation using Gson in Java?
- How to use @Until annotation using the Gson library in Java?
- How can we use @Since annotation using Gson in Java?
- When to use @JsonManagedReference and @JsonBackReference annotations using Jackson in Java?
- When to use @JsonAutoDetect annotation in Java?
- JSON Schema Support using Jackson in Java?
- Pretty print JSON using Jackson library in Java?

Advertisements