In this tutorial, we will learn how to return XML responses in a Micronaut application using Jackson XML.
Table of contents
- 1. Setting Up the Micronaut Project
- 2. Creating the XML Model
- 3. Creating a Controller to Return XML
- 4. Running and Testing the Application
- 5. Writing Unit Tests
- 6. Download Source Code
- 7. References
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:
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:
<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.
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): Theidfield is serialized as an XML attribute instead of an element.@JacksonXmlProperty(localName = "fullName"): Thenamefield appears as<fullName>instead of<name>.@JacksonXmlProperty: Theagefield retains its default serialization.
3. Creating a Controller to Return XML
Now, let’s create a Micronaut Controller that returns XML data.
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
Personobject 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
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);
}
}