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<>();
        }
    }
liweisb 回答:Java中的不可阻塞方法

IntelliJ警告您,因为您使用的是非阻塞的WebFlux,但也使用RestTemplate的阻塞。您应该改为使用WebClient,这是新的非阻塞方式。

本文链接:https://www.f2er.com/3164665.html

大家都在问