1. 添加依赖
com.squareup.okhttp3 okhttp 4.3.1
2. 声明 RestTemplate
@Componentpublic class WebConfig { /** * 基于OkHttp3配置RestTemplate * @return */ @Bean public RestTemplate restTemplate() { return new RestTemplate(new OkHttp3ClientHttpRequestFactory()); }}
3. 测试controller
@RestControllerpublic class RestTemplateController { @Autowired private RestTemplate restTemplate; @GetMapping("/testRestTemplate") public GraceJSONResult testRestTemplate(Model model) { ResponseEntity responseEntity = restTemplate.getForEntity("http://localhost:8001/article/queryIndexArticleList?page=0&pageSize=10", IMOOCJSONResult.class); Map result = (Map)responseEntity.getBody().getData(); return GraceJSONResult.ok(result); }}