Reputation: 311
I am new to Sping Boot and I am trying to call External POST API using RestTemplate but getting error No HttpMessageConverter for SampleRequest.
Below is my Controller class I have also tried converting the message but it did not helped me.
Controller Class :
import com.SampleRequest;
@RestController
@RequestMapping(path = "/api")
public class TicketAgentController {
private static final Logger LOGGER = LogManager.getLogger(TicketAgentController);
@PostMapping(path = "/TicketAgent", consumes = "application/json", produces = "application/json")
public String createTroubleTicket(@RequestBody SampleRequest request) throws URISyntaxException {
//Move to Common and service
RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(getMessageConverters());
final String baseUrl = "http://172.100.5.89:8095/api/Ticket/v2";
URI uri = new URI(baseUrl);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
LOGGER.info("Start ..");
String result = restTemplate.postForObject(uri, request, String.class);
LOGGER.info("Result :" + result);
LOGGER.info("End ..");
return result;
}
//Move to Common
private List<HttpMessageConverter<?>> getMessageConverters() {
List<HttpMessageConverter<?>> converters =
new ArrayList<HttpMessageConverter<?>>();
converters.add(new MappingJackson2HttpMessageConverter());
return converters;
}
}
Exception After running :
2019-12-17 12:11:38.943 ERROR 9240 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.RestClientException: No HttpMessageConverter for com.SampleRequest with root cause
org.springframework.web.client.RestClientException: No HttpMessageConverter for com.SampleRequest at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:964) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE] at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE] at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:717) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE] at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:443) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Let me know if I need to provide anything more.
Upvotes: 1
Views: 7889
Reputation: 109
I faced the same issue on debugging I found that I haven't defined getter methods for the sample request pojo which were leading me to the same error.
Upvotes: 5
Reputation: 821
RestTemplate does not know anything about your object SampleRequest and its serialization. I think the simplest way to use ObjectMapper:
...
var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
var entity = new HttpEntity<>(mapper.writeValueAsString(request), headers);
var result = restTemplate.postForObject(uri, entity, String.class);
...
You can Autowire ObjectMapper mapper or create new one.
Upvotes: 2