如果我有 HTTP REST API:example /getCount
示例代码
@RestController
public class CountController {
long count = 0;
@Getmapping("/getCount")
public long getCount(){
count++; // Here is the problem, how to keep the latest count if the API was hit by multiple Application/clients
return count;
}
}
问题描述:任何应用/系统每次调用上述 API 时,计数都会加 1,调用者实体可以获得准确的命中次数。
健康)状况:它是一个开放的API。需要指导如何实现它。
这是我遇到的唯一问题陈述
如果您想要计数器行为,可以使用静态类型,如下所示
- 计数器对于所有调用 API 的客户端都是通用的。
- 服务器重新启动后,计数器将重新重置为 0。
@RestController
public class CountController {
static long count = 0;
@Getmapping("/getCount")
public long getCount(){
count++; // Here is the problem, how to keep the latest count if the API was hit by multiple Application/clients
return count;
}
}
如果您希望所有客户端都有不同的计数器,或者即使服务器关闭或重新启动也希望保留计数器值,那么您需要使用数据库来存储计数器。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)