VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • RestTemplate + okhttp 实现远程调用

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);
    }
}

相关教程