Why does presence of @JsonIgnore impacts the XML marshalling?

46 Views Asked by At

I have a common class which contains the fields for marshalling/unmarshalling of XML using the JAXB and serialization/deserialization of JSON using the Jackson library. One of the fields type has the annotation @XmlTransient meaning it should be ignored during the marshalling of XML and @JsonIgnore meaning it should be ignored during the JSON serialization.

But the problem I am facing is that when I have both the annotation and try to marshall the class then I get the type tag in my XML during the marshalling. When I remove the @JsonIgnore and the rest of everything is the same then I do not get the type in my XML.

I am wondering why @JsonIgnore causing the issue during the marshalling? Why presence of @JSONIgnore impact the XML marshalling as both come from different libraries?

Following is the class I have:

Following is my Parent class:

Parent.java:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, visible = true, property = "type")
@JsonSubTypes({
  @JsonSubTypes.Type(value = Child.class, name = "Child")
})
@JsonInclude(Include.NON_NULL)
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
@XmlTransient
@Builder
public class Person implements Serializable {

  @XmlTransient
  @JsonIgnore
  private String type;

  private String name;
  private String age;
}

Following is my Child class which inheris the parent:

@XmlType(
    name = "Child",
    propOrder = {
      "name",
      "age",
      "nickname"
    },
    factoryClass = ObjectFactory.class,
    factoryMethod = "createPerson")
@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    visible = true,
    property = "type")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Child")
@JsonPropertyOrder({
  "type",
  "name",
  "age",
  "nickname"
})
public class Child extends Person implements XmlSupportExtension {
  private String nickname;
}

Following is my interface XmlSupportExtension.java:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    visible = true,
    property = "type")
@JsonSubTypes({
  @JsonSubTypes.Type(value = Child.class, name = "Child")
})
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlSeeAlso({
  Child.class
})
public interface XmlSupportExtension {
  Object xmlSupport();
}

ObjectFactory.java:

import jakarta.xml.bind.annotation.XmlRegistry;

@XmlRegistry
public final class ObjectFactory {
  private ObjectFactory() {}

  // Returns Child
  public static Child createPerson() {
    return new Child();
  }
}

Expected XML during marshalling:

<child>
 <name>Batman</name>
 <age>50</age>
 <nickname>Batman</nickname>
</child>

Currently generated XML:

<child>
 <name>Batman</name>
 <age>50</age>
 <nickname>Batman</nickname>
 <type>child</type>
</child>
1

There are 1 best solutions below

2
BATMAN_2008 On

After trying a few things I got to know that the issue was present within the XMLSupportExtension, so I removed the visible = true, and that worked for me. Now adding the @JsonIgnore does not create any problem during the XML marshalling.

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type")
@JsonSubTypes({
  @JsonSubTypes.Type(value = Child.class, name = "Child")
})
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlSeeAlso({
  Child.class
})
public interface XmlSupportExtension {
  Object xmlSupport();
}