【问题标题】:Could not extract response: no suitable HttpMessageConverter found for response type and content type [binary/octet-stream]无法提取响应:没有找到适合响应类型和内容类型的 HttpMessageConverter [binary/octet-stream]
【发布时间】:2021-11-18 09:02:21
【问题描述】:

所以我通过 RestTemplate 使用来自这个 URL 的 JSON 响应 链接:

"https://s3-ap-southeast-1.amazonaws.com/he-public-data/productdf38641.json"

我的产品 POJO:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductModel {

    private String uniq_id;
    private String product_name;
    private int retail_price;
    private int discounted_price;
    private List<String> image;
    private String description;
    private String product_rating;
    private String overall_rating;
    private String brand;
}

现在,当我使用 restTemplate 将这个 Json 对象数组存储在 ProductModel[] 中时。

ProductModel[] books = restTemplate.getForObject(URL, ProductModel[].class);

我收到了这个错误

Caused by: org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.schema.testing.domain.ProductModel] and content type [binary/octet-stream]

当我通过邮递员通过 POST 请求将相同的 JSON 对象传递到 REST 端点时。 它能够处理该请求。 这都是与内容类型相关的游戏吗? 请帮忙,我接下来要做什么。我不知道 。 任何帮助表示赞赏

【问题讨论】:

    标签: spring spring-mvc spring-restcontroller http-message-converter


    【解决方案1】:

    我想这个异常有解决方案。试试看,让我知道结果。
    没有任何消息转换器可以读取您的 HTTP 响应,因此它会失败并出现异常。 这里的主要问题是 content-type,为了克服这个问题,您可以引入自定义消息转换器。并将其注册为各种响应(即忽略响应内容类型标头)。就这样

    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();        
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    
    // We are making this converter to process any kind of response, not only application/*json, which is the default behaviour
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));        
    messageConverters.add(converter);  
    restTemplate.setMessageConverters(messageConverters); 
    

    【讨论】:

      猜你喜欢
      • 2016-03-15
      • 2020-02-12
      • 2014-09-03
      • 2019-09-09
      • 2017-01-22
      • 2017-10-31
      • 1970-01-01
      • 2013-07-05
      • 2018-08-18
      相关资源
      最近更新 更多