In this tutorial, we will learn how to return JSON in a Micronaut controller using simple and practical examples.
Table of contents
- 1. Setting Up the Micronaut Project
- 2. Returning a JSON Object
- 3. Returning a List of JSON Objects
- 4. Writing Unit Tests for the Controller
- 5. Download Source Code
- 6. References
Technologies used:
- Java 21
- Micronaut 4.7.6
- Maven 3.9
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.returnjson --build=maven --lang=java
Alternatively, we can use Micronaut Launch to generate our project via a web interface.
2. Returning a JSON Object
Micronaut provides built-in support for JSON serialization using Jackson. We can return JSON responses simply by returning an object from a controller method.
Define a Data Model
Let’s create a simple Java class that will be returned as JSON:
package com.mkyong.model;
import io.micronaut.serde.annotation.Serdeable;
@Serdeable
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
}
What is `@Serdeable`?
The @Serdeable annotation indicate that a class can be serialized and deserialized automatically.
Create a Controller
Now, we create a controller that returns a User object as JSON:
package com.mkyong;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.MediaType;
@Controller("/users")
public class UserController {
@Get(produces = MediaType.APPLICATION_JSON)
public User getUser() {
return new User("Mkyong", 42);
}
}
Run the Application
Run the application:
./mvnw mn:run
Visit this end point:
http://localhost:8080/users
The response will be:
{
"name": "Mkyong",
"age": 42
}
3. Returning a List of JSON Objects
We can also return a list of objects instead of a single object:
package com.mkyong.controller;
import com.mkyong.model.User;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import java.util.List;
@Controller("/users")
public class UserController {
@Get(produces = MediaType.APPLICATION_JSON)
public User getUser() {
return new User("Mkyong", 42);
}
@Get(uri = "/all", produces = MediaType.APPLICATION_JSON)
public List<User> getUsers() {
return List.of(
new User("Mkyong", 42),
new User("Zi Lap", 10)
);
}
}
Run the Application
Run the application:
./mvnw mn:run
Visit this endpoint:
http://localhost:8080/users/all
The response will be:
[
{
"name": "Mkyong",
"age": 42
},
{
"name": "Zi Lap",
"age": 10
}
]
4. Writing Unit Tests for the Controller
Let’s write test cases to verify the JSON responses from the UserController:
package com.mkyong;
import com.mkyong.model.User;
import io.micronaut.core.type.Argument;
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 org.junit.jupiter.api.Test;
import jakarta.inject.Inject;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@MicronautTest
public class UserControllerTest {
@Inject
@Client("/")
HttpClient client;
@Test
void testGetUser() {
HttpRequest<String> request = HttpRequest.GET("/users");
HttpResponse<User> response = client.toBlocking().exchange(request, User.class);
assertEquals(200, response.getStatus().getCode());
assertNotNull(response.body());
assertEquals("Mkyong", response.body().getName());
assertEquals(42, response.body().getAge());
}
@Test
void testGetAllUsers() {
HttpRequest<String> request = HttpRequest.GET("/users/all");
HttpResponse<List<User>> response = client.toBlocking().exchange(request, Argument.listOf(User.class));
assertEquals(200, response.getStatus().getCode());
assertNotNull(response.body());
assertEquals(2, response.body().size());
assertEquals("Mkyong", response.body().get(0).getName());
assertEquals(42, response.body().get(0).getAge());
assertEquals("Zi Lap", response.body().get(1).getName());
assertEquals(10, response.body().get(1).getAge());
}
}
These tests verify that both /users and /users/all return the expected JSON responses.