月半花开 2019-03-01 11:36 采纳率: 0%
浏览 1289

能解释下RestTemplate的作用,或者实际运用场景

能解释下RestTemplate的作用吗?是用来调用接口的同时对外开放接口?也就是中转接口服务,看了很多例子,都是可以调用api接口,同时,自己也可以在浏览器中进行访问传参,这个是为了和前端进行交互?

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RestTemplateController {
    @Autowired
    private RestTemplate restTemplate;

    /*// Restful服务对应的url地址,解决硬编码问题
    @Value("${user.userServicePath}")
    private String userServicePath;*/

    @GetMapping("/template/{id}")
    public User findById(@PathVariable Long id) {
        // http://localhost:7900/user/是前面服务的对应的url
        User u=new User();
        u = this.restTemplate.getForObject("http://localhost:7900/user/" + id, User.class);

//      u.setId(id);
        System.out.println(u);
        return u;
    }
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-03-01 12:00
    关注

    对,让前端api统一并且方便理解,不知道你是不理解Restful这种api风格,还是不理解RestTemplate

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog