简体   繁体   中英

Jackson JSON serialization mapping

I have two java classes A and B that I would like to be able to serialize to JSON such that their content looks the same to a consumer.

I ommitted the constructors/getters/setters to keep it minimal.

public class A {

    @JsonProperty("b")
    private B b;

}

public class B {

   @JsonProperty("propertyB")
   public String propertyB;

}

When I serialize AI get

{
   b: {
       propertyB: ''
   }
}

But I want it to look like B's serialization:

{
  propertyB: ''
}

Is there any way to achieve that simply by configuring the serialization process respectively, ie using jackson annotations or some other form of jackson configuration.

Thanks

You can use @JsonUnwrapped

public class A {
    @JsonUnwrapped
    private B b;
}

https://fasterxml.github.io/jackson-annotations/javadoc/2.2.0/com/fasterxml/jackson/annotation/JsonUnwrapped.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM