admin管理员组

文章数量:1794759

在spring MVC中使用RestTemplate

在spring MVC中使用RestTemplate

1. 配置

在程序入口中添加RestTemplate的配置:

@Configuration public static class springConfig { @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } }

2. 在controller中添加webapi接口

@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET) public User getUserInfo(@PathVariable("userId") String userId) { //实际调用中一般返回值是从数据库中获取的 return new User(); } 3.在需要引用的类中调用 RestTemplate restTemplate = new RestTemplate(); return restTemplate.getForObject("localhost:8080/user/{userId}", User.class); 参考 blog.csdn/dyllove98/article/details/41284675

本文标签: springMVCRestTemplate