java - Micronaut - @Controller 与 @Endpoint
问题描述
我不明白何时应该使用@Controller
以及何时@Endpoint
使用 Micronaut 框架创建端点。
按照文档,我创建了一个服务并以/endpoint
这种方式使其可用:
@Controller("/endpoint")
public class DummyService {
@Get
@Produces(MediaType.TEXT_PLAIN)
public String index() {
return "Hello World!";
}
}
但这里它是用@Endpoint
注释创建的:
@Endpoint("/endpoint")
public class DummyService {
@Get
@Produces(MediaType.TEXT_PLAIN)
public String index() {
return "Hello World!";
}
}
在 Micronaut 的端点上创建服务并使其可用的正确方法是什么?
如果这个问题是由于对更基本的概念缺乏理解而引起的,你能给我提供参考吗?
解决方案
@Endpoint
应该用于管理端点(调整日志级别、管理缓存、监控资源利用率等),而不是应用程序功能。 @Controller
应该用于不属于管理和监控的应用程序端点。
编辑
为了解决具体提出的问题:
在 Micronaut 的端点上创建服务并使其可用的正确方法是什么?
通常,这样做的方法是将 bean 添加到作为您的服务的应用程序上下文中,然后让 DI 容器在需要的地方注入该 bean。
@Singleton
public class SomeService {
// ...
}
@Controller
public class SomeController {
private final SomeService someService;
public SomeController(SomeService someService) {
this.someService = someService;
}
// ...
}
@Endpoint
public class SomeManagementEndpoint {
private final SomeService someService;
public SomeManagementEndpoint(SomeService someService) {
this.someService = someService;
}
// ...
}
推荐阅读
- f# - F# 类型 'a -> ('a -> 'b) -> 'b
- math - 如何解决 T(n+1)=T(n)+logn 的复杂度
- php - 无法使用 PHP ARC2 SPARQL 从土地注册 api 数据中获取数据
- python - 在 Python 中拆分 Json 数组
- matlab - 如何在 MATLAB 中通过 UDP 发送大量数字?
- c++11 - 如何指定整数数组作为编译器命令行定义 -D
- android - 如何使用适用于 Android 和 iOS 的 Delphi 10.3 (Firemonkey) 应用程序检查蓝牙适配器的状态(开/关)
- postgresql - 无法使用 psql 连接到 Pgbouncer
- html - 为什么我的 HERO 不显示带有文本的图像?
- gradle - Gradle 的 Script 对象 Logger 配置在哪里?