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?
Convert your RestResponsePage
Java class into a Kotlin class.