Return XML in a Micronaut Controller

In this tutorial, we will learn how to return XML responses in a Micronaut application using Jackson XML.

Table of contents

Technologies used:

  • Java 21
  • Micronaut 4.7.6
  • Maven 3.9
  • Jackson XML

1. Setting Up the Micronaut Project

We can create a Micronaut project using the Micronaut CLI. Open the terminal and run the following command:

Terminal

mn create-app com.mkyong.returnxml --build=maven --features=jackson-xml

This command will generate a Micronaut application with Jackson XML support enabled.

Add Jackson XML Dependency

If the existing project does not have XML support enabled, add the following dependency to the pom.xml:

pom.xml

<dependency>
    <groupId>io.micronaut.xml</groupId>
    <artifactId>micronaut-jackson-xml</artifactId>
    <scope>compile</scope>
</dependency>

2. Creating the XML Model

Let’s create a Person model that will be serialized into XML.

Person.java

package com.mkyong;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@JacksonXmlRootElement(localName = "person")
public class Person {

    @JacksonXmlProperty(isAttribute = true)
    private int id;

    @JacksonXmlProperty(localName = "fullName")
    private String name;

    @JacksonXmlProperty
    private int age;

    public Person() {
    }

    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Explanation:

  • @JacksonXmlProperty(isAttribute = true): The id field is serialized as an XML attribute instead of an element.
  • @JacksonXmlProperty(localName = "fullName"): The name field appears as <fullName> instead of <name>.
  • @JacksonXmlProperty: The age field retains its default serialization.

3. Creating a Controller to Return XML

Now, let’s create a Micronaut Controller that returns XML data.

PersonController.java

package com.mkyong;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;

@Controller("/person")
public class PersonController {

    @Get
    @Produces(MediaType.APPLICATION_XML)
    public Person getPerson() {
        return new Person(1, "Mkyong", 42);
    }
}
  • We use @Produces(MediaType.APPLICATION_XML) to specify that this endpoint returns XML.
  • The Micronaut framework automatically serializes the Person object into XML format.

4. Running and Testing the Application

Run the application using Maven:


./mvnw mn:run

Visit this endpoint


http://localhost:8080/person

Output


<person id="1">
    <age>42</age>
    <fullName>Mkyong</fullName>
</person>

5. Writing Unit Tests

PersonControllerTest.java

package com.mkyong;

import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@MicronautTest
class PersonControllerTest {

    @Inject
    @Client("/")
    HttpClient client;

    @Test
    void testGetPersonReturnsXml() {
        HttpRequest<?> request = HttpRequest.GET("/person")
                .accept("application/xml");

        HttpResponse<String> response = client.toBlocking().exchange(request, String.class);

        assertTrue(response.getBody().isPresent());

        String xml = response.getBody().get();
        String expected = "<person id=\"1\"><age>42</age><fullName>Mkyong</fullName></person>";
        assertEquals(expected, xml);
    }
}

6. Download Source Code

https://github.com/mkyong/micronaut.git

cd returnxml

./mvnw mn:run

7. References

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments