Micronaut REST APIs Interview Questions and Answers
Master Micronaut REST APIs with interview questions covering controllers, routing, HTTP methods, request mapping, validation, exception handling, content negotiation, and production best practices.
Micronaut REST APIs Interview Questions and Answers
Introduction
REST (Representational State Transfer) is the most widely used architectural style for building web services. Micronaut provides a lightweight, annotation-driven programming model for developing high-performance REST APIs with minimal memory usage and fast startup.
Micronaut offers built-in support for:
- REST Controllers
- HTTP Routing
- JSON Serialization
- Request Validation
- Exception Handling
- Content Negotiation
- Dependency Injection
- Reactive Programming
REST APIs built using Micronaut are commonly deployed in Kubernetes, Docker, AWS Lambda, and enterprise microservices.
REST API Architecture
flowchart LR
Client --> APIController
APIController --> Service
Service --> Repository
Repository --> Database
Service --> Kafka
Service --> Redis
Q1. What is a REST API?
Answer
A REST API is a web service that communicates over HTTP using resources identified by URLs.
Common HTTP methods include:
| Method | Purpose |
|---|---|
| GET | Retrieve data |
| POST | Create data |
| PUT | Replace data |
| PATCH | Partial update |
| DELETE | Delete data |
Example
GET /customers/101
POST /customers
PUT /customers/101
DELETE /customers/101
Q2. How do you create a REST Controller in Micronaut?
Micronaut uses the @Controller annotation.
Example
@Controller("/customers")
public class CustomerController {
@Get("/{id}")
public Customer find(Long id) {
return new Customer(id, "John");
}
}
Request
GET /customers/1
Response
{
"id":1,
"name":"John"
}
Q3. How does Routing work?
Routes map incoming URLs to controller methods.
Example
@Controller("/products")
public class ProductController {
@Get
public List<Product> all() {
}
@Get("/{id}")
public Product find(Long id) {
}
}
Routing Flow
flowchart LR
HTTPRequest --> Router
Router --> Controller
Controller --> Service
Service --> Response
Q4. How are Request Parameters handled?
Path Variable
@Get("/{id}")
public Customer find(Long id)
Request
/customers/100
Query Parameter
@Get
public List<Customer> search(
@QueryValue String city)
Request
/customers?city=Dallas
Header
@Get
public String token(
@Header("Authorization")
String token)
Q5. How do you accept Request Bodies?
POST requests usually receive JSON.
DTO
public class CustomerRequest {
private String name;
private String email;
}
Controller
@Post
public Customer save(
@Body CustomerRequest request){
return service.save(request);
}
JSON
{
"name":"David",
"email":"[email protected]"
}
Q6. How does Validation work?
Micronaut supports Jakarta Bean Validation.
DTO
public class CustomerRequest {
@NotBlank
private String name;
@Email
private String email;
}
Controller
@Post
public Customer save(
@Valid
@Body CustomerRequest request){
return service.save(request);
}
Benefits
- Automatic validation
- Cleaner code
- Standard validation rules
Q7. How is Exception Handling implemented?
Global exception handling uses @Error.
Example
@Error(global = true)
public HttpResponse<String> handle(
Exception ex){
return HttpResponse
.badRequest(ex.getMessage());
}
Exception Flow
sequenceDiagram
Client->>Controller: Request
Controller->>Service: Execute
Service-->>Controller: Exception
Controller-->>Client: HTTP 400
Q8. What is Content Negotiation?
Micronaut automatically supports multiple content types.
Example
@Controller("/users")
@Produces(MediaType.APPLICATION_JSON)
Consumes
@Consumes(MediaType.APPLICATION_JSON)
Supported formats
- JSON
- XML
- Plain Text
- Custom Media Types
Q9. How does Micronaut process a REST Request?
flowchart TD
Client --> HTTPServer
HTTPServer --> Router
Router --> Controller
Controller --> Service
Service --> Repository
Repository --> Database
Database --> Repository
Repository --> Service
Service --> Controller
Controller --> Client
This entire flow is managed by the Micronaut runtime.
Q10. REST API Best Practices
Use Proper HTTP Status Codes
| Status | Meaning |
|---|---|
| 200 | Success |
| 201 | Created |
| 204 | No Content |
| 400 | Bad Request |
| 401 | Unauthorized |
| 404 | Not Found |
| 500 | Internal Server Error |
Validate Input
Always validate incoming requests.
Return DTOs
Avoid exposing entities directly.
Use Global Exception Handling
Keep controllers clean.
Version APIs
Example
/api/v1/customers
/api/v2/customers
Banking Example
flowchart TD
MobileApp --> ApiGateway["API Gateway"]
ApiGateway["API Gateway"] --> CustomerController
CustomerController --> CustomerService
CustomerService --> CustomerRepository
CustomerRepository --> Database
CustomerService --> Kafka
CustomerService --> AuditService
Common Interview Questions
- What is a REST API?
- What is
@Controller? - Explain HTTP methods.
- Difference between Path Variable and Query Parameter?
- How does
@Bodywork? - How does validation work?
- Explain global exception handling.
- What is content negotiation?
- How does Micronaut route requests?
- REST API best practices?
Quick Revision
| Topic | Summary |
|---|---|
| Controller | Handles HTTP requests |
| GET | Retrieve resource |
| POST | Create resource |
| PUT | Update resource |
| DELETE | Remove resource |
| @Body | Reads JSON request |
| @Valid | Validates request |
| @Error | Global exception handler |
| DTO | API request/response model |
| HTTP Status | Indicates request result |
REST Request Lifecycle
sequenceDiagram
Client->>HTTP Server: HTTP Request
HTTP Server->>Router: Match Route
Router->>Controller: Invoke Method
Controller->>Service: Business Logic
Service->>Repository: Database Call
Repository-->>Service: Result
Service-->>Controller: Response DTO
Controller-->>Client: JSON Response
Key Takeaways
- Micronaut provides a lightweight, annotation-driven framework for building RESTful web services.
- Use
@Controllerto define REST endpoints and HTTP method annotations such as@Get,@Post,@Put, and@Deleteto map requests. - Request data can be received through path variables, query parameters, headers, and request bodies.
- Use
@Bodyto deserialize JSON requests into Java objects. - Integrate Jakarta Bean Validation with
@Validto automatically validate incoming requests. - Centralize error handling using
@Errorfor cleaner controllers and consistent error responses. - Support multiple media types through content negotiation using
@Producesand@Consumes. - Return DTOs instead of JPA entities to improve API security and maintainability.
- Use appropriate HTTP status codes and version APIs for long-term compatibility.
- Micronaut REST APIs are well suited for cloud-native, high-performance enterprise microservices running on Docker, Kubernetes, and serverless platforms.