Hi,
I am currently migrating from High Rest API Client to Java APi Client and I got an error which I can not solve.
This is my code:
@Override
public Optional<MyClass> findById(String id, List<String> customerIds) throws IOException {
var searchRequest = RequestsFactory.createDocumentByIdSearchRequest(indexName, id, customerIds);
var searchResponse = elasticsearchJavaApiClient.search(searchRequest, MyClass.class);
return documentTransformer.extractSingleDocumentFromSearchResponse(searchResponse);
}
If I use ObjectNode.class instead of MyClass.class I get no error.
Previously I had no such error. The error occurs inside elasticsearchJavaApiClient.search()
co.elastic.clients.transport.TransportException: node: https://myDomain:9200/, status: 200, [es/search] Failed to decode response
...
Caused by: co.elastic.clients.json.JsonpMappingException: Error deserializing co.elastic.clients.elasticsearch.core.search.Hit: jakarta.json.JsonException: Jackson exception (JSON path: hits.hits[0]._source) (line no=1, column no=336, offset=-1)
at co.elastic.clients.json.JsonpMappingException.from0(JsonpMappingException.java:134)
at co.elastic.clients.json.JsonpMappingException.from(JsonpMappingException.java:121)
at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:218)
at co.elastic.clients.json.ObjectDeserializer.deserialize(ObjectDeserializer.java:148)
...
Caused by: jakarta.json.JsonException: Jackson exception
at co.elastic.clients.json.jackson.JacksonUtils.convertException(JacksonUtils.java:39)
at co.elastic.clients.json.jackson.JacksonJsonpMapper$JacksonValueParser.deserialize(JacksonJsonpMapper.java:142)
...
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.OffsetDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 335] (through reference chain: com.example.dto.MyModel["MyField"])
Is the jackson error still the same?
We have an example project in the java client repo where the client is set up to serialize Time classes, you can try comparing your code with ours to see what could be missing (here is where the client is built).
I had written the mapper in the wrong place. Now I fixed it. So my final config:
String[] elasticsearchHosts = elasticsearchEndpoints.split(",");
HttpHost[] httpHosts = Arrays.stream(elasticsearchHosts)
.map(HttpHost::create).toArray(HttpHost[]::new);
// Create the low level client
final RestClientBuilder restClientbuilder = RestClient.builder(httpHosts);
// Set elasticsearch user credentials
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(username, password));
restClientbuilder.setHttpClientConfigCallback(
b -> b.setDefaultCredentialsProvider(credentialsProvider));
RestClient restClient = restClientbuilder.build();
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaTimeModule())
.build();
// Create the transport with a Jackson mapper which maps classes to json
ElasticsearchTransport transport = new RestClientTransport(restClient,
new JacksonJsonpMapper(mapper));
return new ElasticsearchClient(transport);
But additionally I had to add lombok annotations to MyClass.class
@Getter
@ToString
@NoArgsConstructor
The reason was that I had two OffsetDateTime fields and could not be parsed. But it works now.
Thank you very much for the help.
Sincerely,
Murat
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.