1. Home
  2. Docs
  3. Java Micronaut Examples
  4. Java Micronaut REST API

Java Micronaut REST API

Time: 45 minutes

What you’ll learn

How to setup your application for : 

  • getting data from REST API, 
  • providing data to REST API. 

In this tutorial, we will create a simple Java component with the Java Micronaut scaffolder. We want to expose the single REST endpoint for getting user details for his username only. This will require simple sequential orchestration of two REST services, one to get user authorization privileges and second for basic user details. 

Project source

This example project can be cloned from: [email protected]:innobank/client-info-service.git



Open your IDE, import the created component and start coding:

  • Define the message payload. Here is an example of the GetClientInfoResponse, which is a simple POJO with basic user details and roles:
    • Generate getters and setters with your IDE.
    • Examples of GetClientDataResponse and GetClientRolesResponse can be found in the example project repository.
package io.codenow.client.info.service.model;

import java.time.LocalDate;
import java.util.Set;

public class GetClientInfoResponse {

    private String firstname;
    private String surname;
    private LocalDate birthdate;
    private Set<String> roles;
package io.codenow.client.info.service.clients;

import io.codenow.client.info.service.model.GetClientDataResponse;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.PathVariable;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.Single;

public interface ClientDataServiceClient {

    public Single<GetClientDataResponse> getClientData(@PathVariable String username);
package io.codenow.client.info.service.controller;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.codenow.client.info.service.clients.ClientAuthorizationServiceClient;
import io.codenow.client.info.service.clients.ClientDataServiceClient;
import io.codenow.client.info.service.model.GetClientDataResponse;
import io.codenow.client.info.service.model.GetClientInfoResponse;
import io.codenow.client.info.service.model.GetClientRolesResponse;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.PathVariable;
import io.micronaut.http.annotation.Produces;
import io.micronaut.validation.Validated;
import io.reactivex.Single;

public class ClientInfoController {

	private static final Logger LOG = LoggerFactory.getLogger(ClientInfoController.class);
	private ClientAuthorizationServiceClient clientAuthorizationServiceClient;
	private ClientDataServiceClient clientDataServiceClient;
	public ClientInfoController(ClientAuthorizationServiceClient clientAuthorizationServiceClient,
   		 ClientDataServiceClient clientDataServiceClient) {
   	 this.clientAuthorizationServiceClient = clientAuthorizationServiceClient;
   	 this.clientDataServiceClient = clientDataServiceClient;

	public Single<GetClientInfoResponse> getClientInfo(@PathVariable String username) {
   	 LOG.info("username: {}", username);

   	 Single<GetClientRolesResponse> rolesResp = clientAuthorizationServiceClient.getClientRoles(username);
   	 Single<GetClientDataResponse> clientDataResp = clientDataServiceClient.getClientData(username);
   	 Set<String> roles = rolesResp.blockingGet().getRoles();
   	 GetClientInfoResponse response = new GetClientInfoResponse();
   	 GetClientDataResponse info = clientDataResp.blockingGet();

    	return Single.just(response);
  • Do not forget to change the swagger.yaml. Check it in the example project: src/main/resources/META-INF/swagger/swagger.yaml
  • Try to build and run the application in your IDE. After startup, you should be able to access your new controller’s swagger: http://localhost:8080/swagger/index.html
    • For the correct setup, check the Readme.md in the project root or see the tutorial  Java Micronaut Local Development, section Prepare local development IDE
    • The component exposes a simple GET endpoint and can be tested using a browser call only. You should not forget to document your API and use the swagger-ui accordingly.

What’s next?

If your code works in the local development, you are ready to push your changes to GIT and try to build and deploy your new component version to the CodeNOW environment. For more information, see Application Deployment and Monitoring.

See our other developer guides to get orchestrated microservices Micronaut REST Server with PostgreSQL and Micronaut REST Server with Redis and Kafka working.

Was this article helpful to you? Yes No