Custom 404 Response - Micronaut Typing CST Test
Loading…
Custom 404 Response — Micronaut Code
Custom error handling for 404 requests.
import io.micronaut.http.annotation.*;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.server.exceptions.ExceptionHandler;
import javax.inject.Singleton;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpStatus;
import io.micronaut.web.router.exceptions.NoRouteFoundException;
@Singleton
class NotFoundHandler implements ExceptionHandler<NoRouteFoundException, HttpResponse> {
@Override
public HttpResponse handle(HttpRequest request, NoRouteFoundException exception) {
return HttpResponse.status(HttpStatus.NOT_FOUND).body("Custom 404 Not Found");
}
}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.
Quick Explain
- ▸Micronaut is designed for microservices and serverless computing, with fast startup and minimal memory overhead.
- ▸It uses compile-time dependency injection and AOP to avoid reflection and runtime proxies.
- ▸Supports reactive programming and non-blocking HTTP clients/servers.
- ▸Built-in support for cloud-native features like service discovery, configuration, and distributed tracing.
- ▸Works with Java, Kotlin, and Groovy on the JVM.
Core Features
- ▸Annotation-based dependency injection
- ▸Reactive HTTP client and server
- ▸Integration with GraalVM for native images
- ▸Declarative configuration and environment profiles
- ▸Built-in service discovery and security features
Learning Path
- ▸Learn Java or Kotlin basics
- ▸Understand dependency injection and AOP
- ▸Build simple Micronaut REST APIs
- ▸Implement reactive endpoints
- ▸Deploy microservices and serverless functions
Practical Examples
- ▸REST API for e-commerce service
- ▸Reactive streaming service
- ▸Serverless function deployed to AWS Lambda
- ▸IoT device backend with low memory footprint
- ▸Microservice using service discovery and config server
Comparisons
- ▸Micronaut vs Spring Boot: Micronaut has compile-time DI, Spring Boot is runtime reflection-based
- ▸Micronaut vs Quarkus: Both are low-memory, fast startup frameworks, Quarkus integrates more tightly with GraalVM
- ▸Micronaut vs Node.js: Micronaut is JVM-based, typed, and compiled; Node.js is interpreted and single-threaded
- ▸Micronaut vs Ktor: Micronaut is full-stack microservice-focused; Ktor is lightweight Kotlin web framework
- ▸Micronaut vs Vert.x: Micronaut emphasizes DI and microservices; Vert.x focuses on reactive event-driven applications
Strengths
- ▸Extremely fast startup and low memory usage
- ▸Strongly typed DI at compile-time, avoiding runtime reflection
- ▸Well-suited for microservices and serverless environments
- ▸Reactive programming support
- ▸Integration-ready for cloud-native infrastructure
Limitations
- ▸Smaller ecosystem compared to Spring Boot
- ▸Learning curve for compile-time DI and AOP
- ▸Limited community resources compared to older frameworks
- ▸Some advanced features require understanding of Micronaut internals
- ▸Not ideal for heavy monolithic applications
When NOT to Use
- ▸Monolithic applications requiring heavy frameworks
- ▸Small projects without cloud or microservice needs
- ▸Teams unfamiliar with JVM ecosystem
- ▸Projects needing a large library ecosystem (Spring Boot is bigger)
- ▸Quick prototypes where startup time is less critical
Cheat Sheet
- ▸mn create-app my-app -> generate project
- ▸@Controller('/path') -> define controller
- ▸@Get('/endpoint') -> define GET route
- ▸@Inject MyService -> inject bean
- ▸mn run or ./gradlew run -> start server
FAQ
- ▸Is Micronaut free?
- ▸Yes - open-source under Apache 2.0 license.
- ▸Does Micronaut support reactive programming?
- ▸Yes - reactive HTTP client/server and RxJava/Flow integration.
- ▸Is Micronaut suitable for serverless?
- ▸Yes - fast startup and low memory footprint ideal for serverless.
- ▸Can I use Kotlin with Micronaut?
- ▸Yes - fully supported alongside Java and Groovy.
- ▸Does Micronaut support GraalVM native images?
- ▸Yes - compile-time DI makes it compatible with native images.
30-Day Skill Plan
- ▸Week 1: Java/Kotlin basics and Micronaut CLI
- ▸Week 2: Controllers, Services, and DI
- ▸Week 3: Reactive programming and HTTP clients
- ▸Week 4: Cloud-native integrations and config
- ▸Week 5: Testing, deployment, and performance tuning
Final Summary
- ▸Micronaut is a JVM-based framework for microservices and serverless apps.
- ▸It features fast startup, low memory usage, and compile-time dependency injection.
- ▸Supports reactive programming, cloud-native integration, and GraalVM native images.
- ▸Well-suited for microservices, reactive APIs, and IoT backends.
- ▸Lightweight, modular, and production-ready for modern architectures.
Project Structure
- ▸src/main/java - application source code
- ▸src/main/resources - configuration files
- ▸src/test/java - unit and integration tests
- ▸build.gradle / pom.xml - build configuration
- ▸application.yml - app configuration
Monetization
- ▸Backend for microservice-based SaaS
- ▸Serverless APIs for mobile or web apps
- ▸IoT device data processing
- ▸Reactive streaming services
- ▸Cloud-native solutions for small-to-medium businesses
Productivity Tips
- ▸Use compile-time DI for faster startup
- ▸Leverage reactive programming for high concurrency
- ▸Organize Controllers and Services modularly
- ▸Automate tests and deployments
- ▸Integrate monitoring early in development
Basic Concepts
- ▸Application - main Micronaut app context
- ▸Controller - handles HTTP requests
- ▸Service - business logic component
- ▸Bean - DI component managed by Micronaut
- ▸Reactive streams - for non-blocking data handling