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

RestClientException: Could not write request: no suitable HttpMessageConverter found for request type

我正在尝试使用 RestTemplate 向 REST 服务发送 POST 请求,但出现以下错误

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);

我的 XBrainQueryRequest class 如下

@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;
    }

}

有人能解释一下我为什么会出错以及如何解决它。我是新手。

已解决。将请求参数替换为 objectMapper.writeValueAsString(request)。请求值存在 JSON 格式问题。

旧代码

ResponseEntity<XBrainTradeList> result1 =
    restTemplate.exchange(
        XBrainTradeQueryURL,
        HttpMethod.POST,
        new HttpEntity(request, headers),
        XBrainTradeList.class);

新密码

ResponseEntity<String> rest= 
    restTemplate.exchange(
        XBrainTradeQueryURL,
        HttpMethod.POST, 
        new HttpEntity(objectMapper.writeValueAsString(request), headers), 
        String.class);

我也采用了String格式的回复。

查看spring-web依赖版本,我改成最新的(5.3.5),对我有用!

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.3.5</version>
    </dependency>