I created this restTemplate bean..
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder
.messageConverters(Collections.singletonList(new MappingJackson2HttpMessageConverter()))
.build();
}
as you can see i set the messageConverter.
but when i used the bean inside my service class ... i get an error like this
"RestClientException: Could not write request: no suitable HttpMessageConverter found for request type . . "
here is the code in my service class . .
ResponseEntity<ProcessGroupEntity> response = this.restTemplate.exchange(RequestEntity.post(new URI(uri))
.contentType(MediaType.APPLICATION_JSON)
.body(foo)
, Foo.class);
now i tried setting message converter before calling exchange.
this.restTemplate.setMessageConverters(Collections.singletonList(new MappingJackson2HttpMessageConverter()));
ResponseEntity<ProcessGroupEntity> response = this.restTemplate.exchange(RequestEntity.post(new URI(uri))
.contentType(MediaType.APPLICATION_JSON)
.body(foo)
, Foo.class);
and the code works. so my question is why RestTemplateBuilder's messageConverter() method seems to fail in setting/adding the converter.?
edit:
here is how i inject the restTemplate bean in my service class
private final RestTemplate restTemplate;
...
@Autowired
public SomeService(RestTemplate restTemplate,
...) {
this.restTemplate = restTemplate;
}