
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Importance of @JsonFilter annotation in Java?
The @JsonFilter annotation used to define a custom filter to serialize the Java objects. We need to use the FilterProvider class to define a filter and get the actual filter instance. Now the filter configured by assigning the FilterProvider to ObjectMapper class.
Syntax
@Target(value={ANNOTATION_TYPE,TYPE,METHOD,FIELD,PARAMETER}) @Retention(value=RUNTIME) public @interface JsonFilter
In the below example, the customFilter can be declared as an argument to @JsonFilter annotation extracts only the name and filtering out the other properties of a bean.
Example
import com.fasterxml.jackson.annotation.JsonFilter; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ser.FilterProvider; import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; public class JsonFilterAnnotationTest { public static void main(String args[]) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); FilterProvider filterProvider = new SimpleFilterProvider().addFilter("customFilter", SimpleBeanPropertyFilter.filterOutAllExcept("empName")); String jsonString = mapper.writer(filterProvider).writeValueAsString(new FilterBean()); System.out.println(jsonString); } } @JsonFilter("customFilter") class FilterBean { public int empId = 110; public String empName = "Raja Ramesh"; public String gender = "male"; }
Output
{"empName":"Raja Ramesh"}
- Related Questions & Answers
- Importance of @Override annotation in Java?
- Importance of the Jackson @JsonInclude annotation in Java?
- Importance of @JsonRootName annotation using Jackson in Java?
- Importance of @JsonView annotation using Jackson in Java?
- Importance of @JsonIdentityInfo annotation using Jackson in Java?
- Importance of @JsonUnwrapped annotation using Jackson in Java?
- Importance of HashSet in Java
- Importance of StringReader class in Java?
- Importance of clone() method in Java?
- Importance of isDaemon() method in Java?
- Importance of XOR operator in Java?
- Importance of return type in Java?
- Importance of yield() method in Java?
- Importance of SerialVersionUID keyword in Java?
- Importance of join() method in Java?
Advertisements