java RestClientException:无法写入请求:找不到适合请求类型的 HttpMessageConverter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45501630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RestClientException: Could not write request: no suitable HttpMessageConverter found for request type
提问by Datta
I am trying to send a POST request to REST service using RestTemplate but getting below error
我正在尝试使用 RestTemplate 向 REST 服务发送 POST 请求,但出现以下错误
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.
有人可以解释我为什么会出错以及如何解决它。我对这些东西很陌生。
回答by Datta
Resolved. Replaced request paramter with objectMapper.writeValueAsString(request)
. There was a JSON
format issue with request value.
解决。将请求参数替换为objectMapper.writeValueAsString(request)
. 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
格式的回应。