微服务那点事

今天用到了RestTemplate,提供了多种便捷远程访问http服务的方法。br/>是一种简洁的访问restful服务模板类,是spring提供的用于访问客户端的模板工具集。
使用时要携带三个参数:(url,requestMap,ResponseBean.class)
分别是rest的请求地址,传入的参数,http响应转换被转换的类型
@Configuration
public class Configbean {

@Bean
public RestTemplate getRestTemplate() {
    return new RestTemplate();
}

controller层

@Autowired
private RestTemplate restTemplate;

@RequestMapping("/consumer/dept/add")
public boolean add(Dept dept) {

    return restTemplate.postForObject(REST_URL_PREFIX, dept, Boolean.class);
}

详细可进入官网查看:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html

猜你喜欢

转载自blog.51cto.com/12112646/2293923