首页 > 解决方案 > 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<>();
        }
    }

标签: javaspring-bootrefactoringspring-webflux

解决方案


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


推荐阅读