[英]RestClientException: Could not write request: no suitable HttpMessageConverter found for request type
I am trying to send a POST request to REST service using RestTemplate but getting below error 我正在尝试使用RestTemplate将POST请求发送到REST服务,但出现以下错误
RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [xxx.query.XBrainQueryRequest] and and content type [application/json].
RestClientException:无法写入请求:找不到适合请求类型[xxx.query.XBrainQueryRequest]和内容类型[application / json]的HttpMessageConverter。
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 我的XBrainQueryRequest类如下
@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.
我是这些东西的新手。
Resolved. 解决。 Replaced request paramter with
objectMapper.writeValueAsString(request)
. 用
objectMapper.writeValueAsString(request)
替换了请求参数。 There was a JSON
format issue with request value. 请求值存在
JSON
格式问题。
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. 我也采取了
String
格式的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.