RestTemplate + okhttp 實現(xiàn)遠程調用
1. 添加依赖
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.3.1</version>
</dependency>
2. 声明 RestTemplate
@Component
public class WebConfig {
/**
* 基于OkHttp3配置RestTemplate
* @return
*/
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
}
3. 测试controller
@RestController
public class RestTemplateController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/testRestTemplate")
public GraceJSONResult testRestTemplate(Model model) {
ResponseEntity<IMOOCJSONResult> responseEntity =
restTemplate.getForEntity("http://localhost:8001/article/queryIndexArticleList?page=0&pageSize=10", IMOOCJSONResult.class);
Map result = (Map)responseEntity.getBody().getData();
return GraceJSONResult.ok(result);
}
}
點擊查看更多內容
風間影月說
去圍觀
創(chuàng)業(yè)公司技術總監(jiān), 10年+開發(fā)和技術管理經(jīng)驗。SUN認證SCJP、PMP、MCP認證。主要從事后端技術和架構領域,有豐富的電商平臺與物流平臺核心系統(tǒng)的架構設計和開發(fā)經(jīng)驗。
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優(yōu)質文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少


贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦