java - java中不可阻塞的方法

标签 java spring-boot refactoring spring-webflux

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/

相关文章:

java - 如何在 Spring boot 中使用非实时数据源创建 Jar 文件?

java.io.FileNotFoundException :(Access is denied) convert byte array to image file

java - 如何在 Spring Boot @Async 中使用 ForkJoinPool?

java - org.springframework.batch.core.JobExecutionException : Partition handler returned an unsuccessful step

java - 使用几乎完全重复的代码但对其他方法的不同调用来重构函数

Android Eclipse - move 项目导致所有 R 引用错误

java - 如何查找java项目中未使用/死的代码

javascript - 如何将 javascript 变量传递给 servlet

java - 线程安全输入验证

java - 带有 `--multi-release` 选项的 jdeps 命令使用失败