0

I have an endpoint

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

the api response looks like

{
  "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
  }
}

I'm trying to consume this endpoint in another micro-service using 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>() {}

The implementation of RestResponsePage was following this post to handle the paged response.

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

However, when running my application I'm getting this error

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

How can I correctly consume this endpoint?

eth4io
  • 288
  • 4
  • 15
  • What do you see in the logs of the service exposing the API? It seems to me that the response is not what you expect it to be. – João Dias Sep 11 '21 at 02:23
  • i tested the api response via a local rest tool (the second code block). it seems okay – eth4io Sep 11 '21 at 02:34
  • Sure, but one thing is calling it via postman for example, another is using Spring. When you call it from the other application what do you see in the logs of the API service? – João Dias Sep 11 '21 at 10:32
  • @JoãoDias thanks. I don't see any errors when calling it from the second spring application. There's also another micro-service consumes the same paged endpoint (not in Java / spring) and works so I didn't think there was error on the serving end – eth4io Sep 11 '21 at 10:39
  • Have you tried this --> https://stackoverflow.com/a/53221890/16572295 ? I found it weird, but maybe you can try it. – João Dias Sep 11 '21 at 11:22
  • Wait a minute, `RestResponsePage` is in Java but your code is written in Kotlin. Is this on purpose? – João Dias Sep 11 '21 at 11:25
  • yeah i used code from that link to make `RestResponsePage`. indeed my code was in Kotlin and `RestResponsePage` was in Java, i think they can be called interchangeably, but I'll try convert it to kotlin – eth4io Sep 11 '21 at 11:30
  • Yeah please do. No need to mix Kotlin and Java unless you have a really good reason to. – João Dias Sep 11 '21 at 11:32
  • Thanks, it worked. I would never thought of this. I guess `not present` does mean it couldn't find the (kotlin) class. Please post an answer I'll accept – eth4io Sep 11 '21 at 11:51

1 Answers1

1

Convert your RestResponsePage Java class into a Kotlin class.

João Dias
  • 16,277
  • 6
  • 33
  • 45