通过 RestTemplate 使用 Spring 分页响应给出不存在错误

Consuming Spring Paged response via RestTemplate gives not present error

我有一个端点

    @GetMapping()
    public Page<String> list(String search, Pageable pageable)

api 响应看起来像

{
  "content": [
    "a00000",
    "b11111"
  ],
  "first": true,
  "last": true,
  "totalPages": 1,
  "totalElements": 2,
  "numberOfElements": 2,
  "size": 2000,
  "number": 0,
  "pageable": {
    "sort": {
      "unsorted": true,
      "sorted": false
    },
    "offset": 0,
    "pageSize": 2000,
    "pageNumber": 0,
    "unpaged": false,
    "paged": true
  }
}

我正在尝试使用 Spring v2.5.0 via

在另一个微服务中使用此端点
  override fun getNamesById(id: String): List<String> {
        val uri = URIBuilder("$baseUrl/api/names")
            .setParameter("search", "id==$id")
            .build()

        try {
            val response = restTemplate.exchange(
                uri,
                HttpMethod.GET,
                null),
                typeReference<RestResponsePage<String>>()
            )

            return response.body!!.content

        } catch (ex: HttpServerErrorException) {
            throw handleServerError(ex)
        }
    }

  inline fun <reified T> typeReference() = object : ParameterizedTypeReference<T>() {}

RestResponsePage 的实施遵循此 post 来处理分页响应。

@JsonIgnoreProperties(ignoreUnknown = true)
public class RestResponsePage<T> extends PageImpl<T> {

    @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
    public RestResponsePage(@JsonProperty("content") List<T> content,
                            @JsonProperty("number") int number,
                            @JsonProperty("size") int size,
                            @JsonProperty("totalElements") Long totalElements) {

        super(content, PageRequest.of(number, size), totalElements);
    }

    public RestResponsePage(List<T> content, Pageable pageable, long total) {
        super(content, pageable, total);
    }

    public RestResponsePage(List<T> content) {
        super(content);
    }

    public RestResponsePage() {
        super(new ArrayList<>());
    }
}

但是,当 运行 我的应用程序出现此错误时

java.lang.TypeNotPresentException: Type com.test.RestResponsePage not present
...
    at com.test.anotherService$getNamesById$$inlined$typeReference.<init>(anotherService.kt:75)
    at com.test.anotherService$getNamesById(anotherService.kt:77)

如何正确使用此端点?

将您的 RestResponsePage Java class 转换为 Kotlin class。