能解释下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;
}
}