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相反,这是新的非阻塞方式。
关于java - java中不可阻塞的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58698788/