
在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👁️ 阅读量:0
© 版权声明:本文《在spring MVC中使用RestTemplate》内容均为本站精心整理或网友自愿分享,如需转载请注明原文出处:https://www.zastudy.cn/wen/1686619087a307171.html。