Java / Micronaut Interview questions
How do you define a REST endpoint in Micronaut?
You define a REST endpoint by annotating a controller method with the appropriate HTTP-verb annotation and mapping any path or body data to method parameters.
@Controller("/books") public class BookController { @Get("/{isbn}") public HttpResponse<Book> findByIsbn(String isbn) { return bookService.find(isbn) .map(HttpResponse::ok) .orElse(HttpResponse.notFound()); } }
The {isbn} path variable binds automatically to the isbn parameter by name. Query parameters bind the same way using @QueryValue when the name doesn't match, request bodies use @Body, and headers use @Header. Returning an HttpResponse<T> lets you control status codes explicitly, while returning a plain object defaults to a 200 response, or another code if you throw a mapped exception.
More Related questions...