简体   繁体   English

RestClientException:无法写入请求:找不到适合请求类型的HttpMessageConverter

[英]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.

相关问题 HttpMessageConverter 异常:RestClientException:无法写入请求:找不到合适的 HttpMessageConverter - HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found org.springframework.web.client.RestClientException:无法写入请求:找不到适合请求类型的 HttpMessageConverter - org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type RestClientException无法提取响应:找不到合适的HttpMessageConverter作为响应类型 - RestClientException Could not extract response: no suitable HttpMessageConverter found for response type 无法写入请求:找不到适合请求类型和内容类型的HttpMessageConverter [application / x-java-serialized-object] - Could not write request: no suitable HttpMessageConverter found for request type and content type [application/x-java-serialized-object] RestTemplate无法编写请求:找不到适合请求类型[java.util.HashMap]的HttpMessageConverter - RestTemplate Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap] Java Android Spring:找不到适合请求类型的HttpMessageConverter - java android spring: no suitable HttpMessageConverter found for request type 找不到适合请求类型 [java.util.LinkedHashMap] 和内容类型 [multipart/form-data] 的 HttpMessageConverter - no suitable HttpMessageConverter found for request type [java.util.LinkedHashMap] and content type [multipart/form-data] java 中的 HTTP POST 引发错误 - 找不到合适的 HttpMessageConverter 将请求正文读取到 object02ABBBA2F2A298EBDCC4 类型 - HTTP POST in java throws an error - no suitable HttpMessageConverter found to read request body into object of type class Android版Spring-无法提取响应:找不到适合响应类型的HttpMessageConverter - Spring for Android - Could not extract response: no suitable HttpMessageConverter found for response type 没有找到适合响应类型的 HttpMessageConverter - no suitable HttpMessageConverter found for response type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM