Spring FrameworkRestTemplate文档中有注释:
\n\n注意:截至
\n5.0此类处于维护模式,仅接受较小的\n更改请求和错误。请考虑使用org.springframework.web.reactive.client.WebClient具有更现代的 API 并支持同步、异步和流式传输的方案。
当我尝试使用WebClient并进行同步调用时,如下所示:
@RestController\n@RequestMapping("/rating")\npublic class Controller {\n\n @Autowired\n private RestTemplate restTemplate;\n\n @Autowired\n private WebClient.Builder webClientBuilder;\n\n @RequestMapping("/{userId}")\n public UserRating getUserRating(@PathVariable("userId") String userId) {\n\n// return restTemplate.getForObject("http://localhost:8083/ratingsdata/user/" + userId, UserRating.class);\n return webClientBuilder.build()\n .get().uri("http://localhost:8083/ratingsdata/user/" + userId)\n .retrieve()\n .bodyToMono(UserRating.class)\n .block();\n\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n项目pom.xml:
\n<?xml version="1.0" encoding="UTF-8"?>\n<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">\n <modelVersion>4.0.0</modelVersion>\n <parent>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-parent</artifactId>\n <version>2.4.0</version>\n <relativePath/> <!-- lookup parent from repository -->\n </parent>\n <groupId>com.daren.tutorial.movie</groupId>\n <artifactId>catalog</artifactId>\n <version>0.0.1-SNAPSHOT</version>\n <name>catalog</name>\n …Run Code Online (Sandbox Code Playgroud)