1.配置类实现
@Configuration
public class WebMvcConfig{
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
2.在容器中使用,注入到controller
@RestController
public class HelloController{
@Autowired
private RestTemplate restTemplate;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); #设置请求头为json
headers.add("user","root");
headers.add("password","root");
restTemplate.postForEntity(...);
}