Issue
I am trying to send a POST request to REST service using RestTemplate but getting below error
RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [xxx.query.XBrainQueryRequest] and and content type [application/json].
XBrainQueryRequest request = new XBrainQueryRequest();
// set query ID
request.setQueryId(XBrainTradequeryId);
request.setFlags(new String[]{"ALL_FIELDS"});
ObjectMapper objectMapper = new ObjectMapper();
logger.info("calling XBrainTradeQuery and Input:{}",objectMapper.writeValueAsString(request));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
try
{
restTemplate = new RestTemplate();
ResponseEntity<XBrainTradeList> result=null;
xBrainTradeList =null;
ResponseEntity<XBrainTradeList> result1 = restTemplate.exchange(XBrainTradeQueryURL, HttpMethod.POST, new HttpEntity(request, headers), XBrainTradeList.class);
and my XBrainQueryRequest class is as below
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class XBrainQueryRequest {
private String queryId;
private String[] flags;
private String[] attributes;
/**
* @return the queryId
*/
public String getQueryId() {
return queryId;
}
public XBrainQueryRequest(String queryId, String[] flags, String[] attributes) {
super();
this.queryId = queryId;
this.flags = flags;
this.attributes = attributes;
}
public XBrainQueryRequest() {
}
public XBrainQueryRequest(String queryId, String[] flags) {
super();
this.queryId = queryId;
this.flags = flags;
}
/**
* @param queryId
* the queryId to set
*/
public void setQueryId(String queryId) {
this.queryId = queryId;
}
public String[] getFlags() {
return flags;
}
public void setFlags(String[] flags) {
this.flags = flags;
}
public String[] getAttributes() {
return attributes;
}
public void setAttributes(String[] attributes) {
this.attributes = attributes;
}
}
Can somebody explain me why I am getting error and how to solve it. I am new to these stuff.
Solution
Resolved. Replaced request paramter with objectMapper.writeValueAsString(request)
. There was a JSON
format issue with request value.
Old code
ResponseEntity<XBrainTradeList> result1 =
restTemplate.exchange(
XBrainTradeQueryURL,
HttpMethod.POST,
new HttpEntity(request, headers),
XBrainTradeList.class);
New code
ResponseEntity<String> rest=
restTemplate.exchange(
XBrainTradeQueryURL,
HttpMethod.POST,
new HttpEntity(objectMapper.writeValueAsString(request), headers),
String.class);
Also I have taken the response in String
format.
Answered By - Datta