1. Home
  2. /
  3. Micronaut
  4. /
  5. Simple Counter API

Simple Counter API - Micronaut Typing CST Test

Loading…

Simple Counter API — Micronaut Code

Demonstrates a simple Micronaut REST API with a counter using Controller and in-memory state.

import io.micronaut.http.annotation.*;
import javax.inject.Singleton;

@Singleton
class CounterService {
	private int count = 0;
	public int getCount() { return count; }
	public int increment() { return ++count; }
	public int decrement() { return --count; }
	public int reset() { count = 0; return count; }
}

@Controller("/counter")
class CounterController {
	private final CounterService service;

	CounterController(CounterService service) {
		this.service = service;
	}

	@Get
	public int getCount() { return service.getCount(); }

	@Post("/increment")
	public int increment() { return service.increment(); }

	@Post("/decrement")
	public int decrement() { return service.decrement(); }

	@Post("/reset")
	public int reset() { return service.reset(); }
}

Micronaut Language Guide

Micronaut is a modern, JVM-based full-stack framework for building modular, easily testable microservices and serverless applications. It emphasizes low memory footprint, fast startup, and compile-time dependency injection.

Primary Use Cases

  • ▸Microservices architecture
  • ▸Serverless applications
  • ▸Reactive APIs and streaming services
  • ▸Cloud-native applications with service discovery
  • ▸IoT backends with low memory footprint

Notable Features

  • ▸Compile-time dependency injection
  • ▸Fast startup and low memory footprint
  • ▸Reactive and non-blocking I/O
  • ▸Cloud-native support (Kubernetes, AWS Lambda, etc.)
  • ▸AOP and declarative HTTP clients

Origin & Creator

Micronaut was created by Object Computing, Inc. (OCI), with Graeme Rocher as the lead architect, first released in 2018.

Industrial Note

Micronaut is optimized for microservices, serverless deployments, and environments where fast startup, low memory usage, and reactive I/O are critical.

More Micronaut Typing Exercises

Micronaut Hello World APIMicronaut JSON EchoMicronaut Query Param ExampleMicronaut Path Variable ExampleMicronaut Async Endpoint ExampleMicronaut Custom 404 ResponseMicronaut Combined Routes ExampleMicronaut Health Check EndpointMicronaut JSON Post Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher