Micronaut 当然可以写 HTTP API,而且体验很直接。
一个常见控制器:
package example;
import io.micronaut.http.annotation.*;
@Controller("/permits")
public class PermitController {
@Get("/{id}")
public PermitView detail(Long id) {
return new PermitView(id, "night-build", "pending");
}
@Post
public PermitView create(@Body CreatePermitCommand command) {
return new PermitView(1L, command.name(), "created");
}
}数据结构:
package example;
import io.micronaut.serde.annotation.Serdeable;
@Serdeable
public record CreatePermitCommand(String name) {
}
@Serdeable
public record PermitView(Long id, String name, String status) {
}这里很有 Micronaut 味道的地方有两个:
- 控制器注解式声明非常清晰
@Serdeable这类序列化元信息也强调显式和编译期友好
你可以把 Controller 理解成:
侦察营前沿哨站。
@Controller("/permits"):这支哨站守哪条路@Get("/{id}"):处理哪类来访@Body:装载单从哪进record + Serdeable:请求和响应的标准军用表格
Micronaut 的 HTTP 层不是“特别花”,但它非常讲究:
声明清晰,运行时轻量。