nean
nean

Reputation: 91

Jackson XML problems on Serializing

I'm having problems on serializing a list of Abstract object, jackson duplicates wrapper on tags.

Here are some results that I obtain :

<visibility>
    <allowed/>
    <autoSubscribed>
        <autoSubscribed>
             <regex attribute="isMemberOf" pattern="\Qxertif\E"/>
        </autoSubscribed>
    </autoSubscribed>
    <obliged>
        <obliged>
            <regex attribute="isMemberOf" pattern="\QQadddsssss\E"/>
        </obliged>
        <obliged>
            <regex attribute="isMemberOf" pattern="\Qabcdef\E"/>
        </obliged>
    </obliged>
</visibility>

or when i remove the @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT, visible = true) from my abstract class i obtain :

<visibility>
    <allowed/>
    <autoSubscribed>
        <autoSubscribed attribute="isMemberOf" pattern="\Qxertif\E"/>
    </autoSubscribed>
    <obliged>
        <obliged attribute="isMemberOf" pattern="\QQadddsssss\E"/>
        <obliged attribute="isMemberOf" pattern="\Qabcdef\E"/>
    </obliged>
</visibility>

This is nearly good but i want the type in the tag. something like :

<visibility>
    <allowed/>
    <autoSubscribed>
        <regex attribute="isMemberOf" pattern="\Qxertif\E"/>
    </autoSubscribed>
    <obliged>
        <regex attribute="isMemberOf" pattern="\QQadddsssss\E"/>
        <regex attribute="isMemberOf" pattern="\Qabcdef\E"/>
    </obliged>
</visibility>

Here are my objects :

@Data // from lombok
public class Visibility implements Serializable {


    private List<VisibilityAbstract> allowed = new ArrayList<>();

    private List<VisibilityAbstract> autoSubscribed = new ArrayList<>();

    private List<VisibilityAbstract> obliged = new ArrayList<>();

}


@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT, visible = true)
@JsonSubTypes({
    @JsonSubTypes.Type(value = VisibilityGroup.class, name = "group"),
    @JsonSubTypes.Type(value = VisibilityRegex.class, name = "regex"),
    @JsonSubTypes.Type(value = VisibilityRegular.class, name = "regular")
})
public abstract class VisibilityAbstract implements Serializable {
}


@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT, visible = true)
@JsonTypeName(value = "group")
public class VisibilityGroup extends VisibilityAbstract implements Serializable {

    @NonNull
    @JacksonXmlProperty(isAttribute = true)
    private String name;

}

All "extends" class have differents properties but are configured like VisibilityGroup.

I'm with spring boot 1.2.2 with jackson 2.4.6 and with woodstox 4.4.1.

On Json all is good, but not in XML.

Any idea on how to solve the problem ?

thanks

Upvotes: 2

Views: 4546

Answers (1)

MajorXbox
MajorXbox

Reputation: 533

This may help.

JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
XmlMapper mapper = new XmlMapper(module);

Upvotes: 1

Related Questions