一、初始配置
在spring boot的启动类中进行配置
@Autowired
private RestTemplateBuilder builder;
@Bean
public RestTemplate restTemplate() {
return builder.build();
}
二、开始使用
在使用RestTemplate的类中自动注入即可
@Autowired
private RestTemplate restTemplate;
三、模拟get请求
在使用RestTemplate的类中自动注入即可
this.restTemplate.getForObject("请求路径"+"携带的参数",预期的返回类型[Object.class]);
例如:
this.restTemplate.getForObject("http://xxx"+xStringBuffer,Object.class);
三、模拟post请求
在使用RestTemplate的类中自动注入即可
this.restTemplate.postForObject("请求路径",携带的实体对象,预期的返回类型[Object.class]);
例如:
this.restTemplate.postForObject("http://xxx",entity,Object.class);
三、模拟delete请求
在使用RestTemplate的类中自动注入即可
restTemplate.exchange(nodeUrl,HttpMethod.DELETE,new HttpEntity<String[]>(ids),String.class);