月半花开 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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作