【问题标题】:What is the correct way to execute a Retrofit call that returns a byte[]?执行返回 byte[] 的 Retrofit 调用的正确方法是什么?
【发布时间】:2022-08-16 02:31:01
【问题描述】:

我正在尝试使用 Retrofit 调用一个方法,该方法将 PDF 作为包装在 Spring ResponseEntity 中的字节 [] 返回。方法基本上是这样的:

@Headers(\"Content-Type: application/pdf\")
@GET(\"/v1/pdf\")
Call<ResponseEntity<byte[]>> getPdf(
   @Query(\"number\") final String number
);

似乎杰克逊不知道如何反序列化这个字节数组,并且在使用时:

final ResponseEntity<byte[]> pdf = service.getPdf(number).execute().body();

我收到以下错误:

Exception in thread \"main\" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.http.ResponseEntity` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

我应该如何利用客户端方法来检索 ResponseEntity?

    标签: java spring retrofit


    【解决方案1】:

    最终更改了服务方法的实现以直接返回 Spring Resource 而不是字节数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 2014-08-04
      • 2018-08-12
      • 2020-06-28
      • 2012-02-25
      相关资源
      最近更新 更多