Create and test a plain-text GET endpoint in Micronaut
Use Micronaut's @Controller and @Get to map GET /hello, then declare text/plain with @Produces(MediaType.TEXTPLAIN). A test annotated with @MicronautTest starts the application context and embedded server, so an HTTP client can verify the response body. This procedure follows the official Java/Gradle guide checked on 2026-11-20; generated project details can vary with the current CLI.
Use Micronaut's @Controller and @Get to map GET /hello, then declare text/plain with @Produces(MediaType.TEXT_PLAIN). A test annotated with @MicronautTest starts the application context and embedded server, so an HTTP client can verify the response body. This procedure follows the official Java/Gradle guide checked on 2026-11-20; generated project details can vary with the current CLI.
Prerequisites and project
The official guide requires JDK 21 or newer and JAVA_HOME configured. With the Micronaut CLI, create the project:
mn create-app example.micronaut.micronautguide --build=gradle --lang=java
cd micronautguideAlternatively, create a Java/Gradle project in Micronaut Launch. Treat the examples below as files to add to the generated project; plugin versions and layout can change.
Add the controller
Create src/main/java/example/micronaut/HelloController.java:
package example.micronaut;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;
@Controller("/hello")
public class HelloController {
@Get
@Produces(MediaType.TEXT_PLAIN)
public String index() {
return "Hello World";
}
}@Controller("/hello") sets the base path; @Get maps GET to it. Since the method returns a string, @Produces explicitly selects text/plain. This is different from an endpoint returning a JSON object.
Test through the embedded server
Add src/test/java/example/micronaut/HelloControllerTest.java:
package example.micronaut;
import static org.junit.jupiter.api.Assertions.assertEquals;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
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;
@MicronautTest
class HelloControllerTest {
@Inject
@Client("/")
HttpClient client;
@Test
void returnsPlainTextGreeting() {
HttpRequest<?> request = HttpRequest.GET("/hello")
.accept(MediaType.TEXT_PLAIN);
String body = client.toBlocking().retrieve(request);
assertEquals("Hello World", body);
}
}@MicronautTest initializes the test application context and embedded server. @Client("/") targets that server. The request asks for plain text, and the assertion checks the exact body.
Run and troubleshoot
Run the Gradle test:
.\gradlew.bat testA successful run completes the Gradle task and creates build/reports/tests/test/index.html. If it fails, use the report to distinguish compilation errors from HTTP response failures. Check package declarations, file paths, test dependencies, route and Accept media type. If the generated template differs, follow its Gradle configuration and the current official guide.
To start the app and send a direct request, use:
.\gradlew.bat run
curl.exe -H "Accept: text/plain" http://localhost:8080/helloThe guide's default port is 8080 and the expected body is Hello World. Use the address in the server log if the port differs. The commands and code are documented examples; they were not run for this article.
Scope and sources
This covers one route's response type and an application-context test. Authentication, databases, deployment, and load testing are out of scope.
[Creating your first Micronaut application (Java / Gradle)](https://guides.micronaut.io/creating-your-first-micronaut-app-gradle-java/)
[Micronaut HTTP server documentation](https://docs.micronaut.io/5.2.x/http/)
[Micronaut Core: dependency injection and application context](https://docs.micronaut.io/5.2.x/core/)