java - java中不可阻塞的方法
问题描述
IntelliJ 说:不恰当的阻塞方法调用
如何重构此方法以通过此标准?
public List<User> getAllUsers(String name) {
Type userList = new TypeToken<List<User>>() {
}.getType();
try {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity
= restTemplate.getForEntity(host, String.class);
Gson gson = new GsonBuilder().create();
List<User> users = gson.fromJson(responseEntity.getBody(), userList );
return users == null ? new ArrayList<>() : users;
} catch (HttpClientErrorException | HttpServerErrorException e) {
return new ArrayList<>();
}
}
解决方案
IntelliJ 警告您,因为您使用的是非阻塞的 WebFlux,但也是RestTemplate
阻塞的。您应该改用WebClient,这是新的非阻塞方式。
推荐阅读
- c++ - '仅将成员函数添加到类的专用模板
- spring - 添加视图文件的位置:SPRING MVC
- apache - 如何使用 TikaConfig 和 Tika 命令行实用程序启用 TesseractOCRParser?
- ios - 错误 ITMS-90171:“无效的捆绑结构 - 二进制文件 'xxxx.app/app/tns_modules/fsevents/lib/binding/Release/node-v47-darwin-x64/fse.node'
- android - 使用 Gson 解析嵌套的 json 数组
- css - 如何将字体转换为实际链接以在 HTML 文档中使用
- rsync - 如何在 OpenStack Swift 中使用密码配置 rsync
- vue.js - 如何在 vue.js 中编写无序列表?
- amazon-web-services - Rails ActiveStorage 与 AWS S3 层
- c++ - C ++ 0x数组到迭代器而不使用开始/结束