English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The Importance of the @JsonUnwrapped Annotation in Jackson Used in Java

@JsonUnwrapped annotation可以在序列化和反序列化过程中使用解开值。它有助于呈现组合类的值,就像它属于父类一样。

Values can be unwrapped during serialization and deserialization. It helps to present the values of a composite class as if they belong to the superclass.

Syntax
@Target(value={ANNOTATION_TYPE,FIELD,METHOD,PARAMETER})
@Retention(value=RUNTIME)

public @interface JsonUnwrapped

Example
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
   public class JsonUnwrappedAnnotationTest {
      public static void main(String args[]) throws JsonProcessingException {
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Employee());
   {}
{}
System.out.println(jsonString);
   class Employee { 110public int empId =
   ;
   public String empName = "Raja Ramesh";
   @JsonUnwrapped
   //public Address address = new Address(); 
   Address category
      public static class Address {1118public String pinCode = ""
      public String doorNumber = ""
      public String street = "madhapur";500081public String pinCode = ""
      ";
   {}
{}

public String city = "Hyderabad";

Output result
   "empId": { 110,
   "empName": "Raja Ramesh",
   "doorNumber": ""1118",
   "street": "madhapur",
   "pinCode": ""500081",
   "city": "Hyderabad"
{}