Jackson XML

1 Introduction

When this library is added to a Micronaut application, it creates the beans necessary to allow for the serialization and deserialization of XML. Both the client and server are supported in a similar manner to the standard support of JSON. Jackson is used to do the conversion of the XML to objects.

Because XML has no array boundaries, the entire request body must be buffered into memory before deserialization can occur. The standard request body size limits apply here as well.

2 Dependency

To use Micronaut Jackson XML add the following dependency:

implementation("io.micronaut.xml:micronaut-jackson-xml")

See the Jackson XML documentation for more information.

3 Example

To send an XML document such as:

<book>
    <name>Huckleberry Finn</name>
</book>

You could define a POJO such as the following:

package io.micronaut.xml.jackson.docs;

import tools.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import io.micronaut.core.annotation.Introspected;

@Introspected
@JacksonXmlRootElement(localName = "book")
public record Book(String name) {
}

To return an XML document such as:

<book isbn="f594e59e-6c2d-41b3-ba77-a3aabafceae4">
    <name>Huckleberry Finn</name>
</book>

You could define a POJO such as the following:

package io.micronaut.xml.jackson.docs;

import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import tools.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import io.micronaut.core.annotation.Introspected;
import org.jspecify.annotations.NonNull;

@JacksonXmlRootElement(localName = "book")
@Introspected
public class BookSaved {

    @NonNull
    private final String name;

    @NonNull
    @JacksonXmlProperty(isAttribute = true)
    private final String isbn;

    public BookSaved(String name, String isbn) {
        this.name = name;
        this.isbn = isbn;
    }

    @NonNull
    public String getName() { return name; }

    @NonNull
    public String getIsbn() { return isbn; }
}

To receive and send XML from a Micronaut Controller, you can specify the Accept HTTP header and the response Content-Type with the @Produces and @Consumes annotations.

package io.micronaut.xml.jackson.docs;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.annotation.Produces;
import java.util.UUID;

@Controller
public class BookController {

    @Consumes(MediaType.APPLICATION_XML)
    @Produces(MediaType.APPLICATION_XML)
    @Post("/book")
    public BookSaved save(@Body Book book) {
        return new BookSaved(book.name(), UUID.randomUUID().toString());
    }
}

Moreover, you can send and receive XML with the Micronaut HTTP Client.

package io.micronaut.xml.jackson.docs;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.client.annotation.Client;

@Client("/")
public interface BookClient {
    @Consumes(MediaType.APPLICATION_XML)
    @Produces(MediaType.APPLICATION_XML)
    @Post("/book")
    BookSaved save(@Body Book book);
}

4 Release History

For this project, you can find a list of releases (with release notes) here:

5 Repository

You can find the source code of this project in this repository:

6 Breaking Changes

Micronaut Jackson XML BOM modules' coordinates have changed from io.micronaut.xml:micronaut-jackson-bom to io.micronaut.xml:micronaut-jackson-xml-bom.