Spring with XML – Return Elements outside of a List

UPDATED ANSWER WITH THE SOLUTION FOR THE CASE:

Simply create a getter for the desired List with the @JacksonXmlElementWrapper(useWrapping = false) annotation above it:

private List<Entry> Entries = new ArrayList<>();

@JacksonXmlElementWrapper(useWrapping = false)
public List<Entry> getEntries() {
    return this.Entries;
}

ORIGINAL ANSWER:

Jackson’s XmlMapper has a special feature to enable or disable wrapping for xml lists.

@Bean
ObjectMapper xmlMapper() {
    return XmlMapper.builder()
        ...
        .defaultUseWrapper(false)
        ...
        .build();
}

If you use the default (autoconfigured) xmlMapper, you can add Jackson2ObjectMapperBuilderCustomizer with the same setting:

@Bean
Jackson2ObjectMapperBuilderCustomizer xmlWrapperCustomizer() {
    return mapperBuilder -> mapperBuilder.defaultUseWrapper(false);
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top