Skip to content

Commit e7e0ac8

Browse files
authored
Migrate to Jackson3.
Jackson2 ist still needed as runtime optional dependency for users that still use the old RestClient. Signed-off-by: Peter-Josef Meisch <pj.meisch@sothawo.com>
1 parent 5821a81 commit e7e0ac8

File tree

11 files changed

+74
-62
lines changed

11 files changed

+74
-62
lines changed

pom.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,32 @@
158158
</dependency>
159159

160160
<!-- Jackson JSON Mapper -->
161+
<dependency>
162+
<groupId>tools.jackson.core</groupId>
163+
<artifactId>jackson-core</artifactId>
164+
<version>3.0.2</version>
165+
</dependency>
166+
<dependency>
167+
<groupId>tools.jackson.core</groupId>
168+
<artifactId>jackson-databind</artifactId>
169+
<version>3.0.2</version>
170+
</dependency>
171+
172+
<!-- Version 2 to use with the legacy RestClient -->
161173
<dependency>
162174
<groupId>com.fasterxml.jackson.core</groupId>
163175
<artifactId>jackson-core</artifactId>
176+
<scope>provided</scope>
177+
<optional>true</optional>
164178
</dependency>
165179
<dependency>
166180
<groupId>com.fasterxml.jackson.core</groupId>
167181
<artifactId>jackson-databind</artifactId>
182+
<scope>provided</scope>
183+
<optional>true</optional>
168184
</dependency>
169185

170186
<!-- CDI -->
171-
172187
<dependency>
173188
<groupId>javax.interceptor</groupId>
174189
<artifactId>javax.interceptor-api</artifactId>

src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchConfiguration.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
import co.elastic.clients.elasticsearch.ElasticsearchClient;
1919
import co.elastic.clients.json.JsonpMapper;
20-
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
20+
import co.elastic.clients.json.jackson.Jackson3JsonpMapper;
2121
import co.elastic.clients.transport.ElasticsearchTransport;
2222
import co.elastic.clients.transport.TransportOptions;
2323
import co.elastic.clients.transport.rest5_client.Rest5ClientOptions;
2424
import co.elastic.clients.transport.rest5_client.low_level.RequestOptions;
2525
import co.elastic.clients.transport.rest5_client.low_level.Rest5Client;
26+
import tools.jackson.databind.cfg.JsonNodeFeature;
27+
import tools.jackson.databind.json.JsonMapper;
2628

2729
import org.springframework.context.annotation.Bean;
2830
import org.springframework.data.elasticsearch.client.ClientConfiguration;
@@ -32,10 +34,6 @@
3234
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
3335
import org.springframework.util.Assert;
3436

35-
import com.fasterxml.jackson.annotation.JsonInclude;
36-
import com.fasterxml.jackson.databind.ObjectMapper;
37-
import com.fasterxml.jackson.databind.SerializationFeature;
38-
3937
/**
4038
* Base class for a @{@link org.springframework.context.annotation.Configuration} class to set up the Elasticsearch
4139
* connection using the Elasticsearch Client. This class exposes different parts of the setup as Spring beans. Deriving
@@ -128,10 +126,11 @@ public JsonpMapper jsonpMapper() {
128126
// we need to create our own objectMapper that keeps null values in order to provide the storeNullValue
129127
// functionality. The one Elasticsearch would provide removes the nulls. We remove unwanted nulls before they get
130128
// into this mapper, so we can safely keep them here.
131-
var objectMapper = (new ObjectMapper())
132-
.configure(SerializationFeature.INDENT_OUTPUT, false)
133-
.setSerializationInclusion(JsonInclude.Include.ALWAYS);
134-
return new JacksonJsonpMapper(objectMapper);
129+
JsonMapper jsonMapper = JsonMapper.builder()
130+
.enable(JsonNodeFeature.WRITE_NULL_PROPERTIES)
131+
.enable(JsonNodeFeature.READ_NULL_PROPERTIES)
132+
.build();
133+
return new Jackson3JsonpMapper(jsonMapper);
135134
}
136135

137136
/**

src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchLegacyRestClientConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
* @since 4.4
4848
* @deprecated since 6.0, use {@link ElasticsearchConfiguration}
4949
*/
50-
@Deprecated(since = "6.0", forRemoval=true)
50+
@Deprecated(since = "6.0", forRemoval = true)
5151
public abstract class ElasticsearchLegacyRestClientConfiguration extends ElasticsearchConfigurationSupport {
5252

5353
/**

src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchConfiguration.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
package org.springframework.data.elasticsearch.client.elc;
1717

1818
import co.elastic.clients.json.JsonpMapper;
19-
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
19+
import co.elastic.clients.json.jackson.Jackson3JsonpMapper;
2020
import co.elastic.clients.transport.ElasticsearchTransport;
2121
import co.elastic.clients.transport.TransportOptions;
2222
import co.elastic.clients.transport.rest5_client.Rest5ClientOptions;
2323
import co.elastic.clients.transport.rest5_client.low_level.RequestOptions;
2424
import co.elastic.clients.transport.rest5_client.low_level.Rest5Client;
25+
import tools.jackson.databind.cfg.JsonNodeFeature;
26+
import tools.jackson.databind.json.JsonMapper;
2527

2628
import org.elasticsearch.client.RestClient;
2729
import org.springframework.context.annotation.Bean;
@@ -32,10 +34,6 @@
3234
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
3335
import org.springframework.util.Assert;
3436

35-
import com.fasterxml.jackson.annotation.JsonInclude;
36-
import com.fasterxml.jackson.databind.ObjectMapper;
37-
import com.fasterxml.jackson.databind.SerializationFeature;
38-
3937
/**
4038
* Base class for a @{@link org.springframework.context.annotation.Configuration} class to set up the Elasticsearch
4139
* connection using the {@link ReactiveElasticsearchClient}. This class exposes different parts of the setup as Spring
@@ -127,10 +125,11 @@ public JsonpMapper jsonpMapper() {
127125
// we need to create our own objectMapper that keeps null values in order to provide the storeNullValue
128126
// functionality. The one Elasticsearch would provide removes the nulls. We remove unwanted nulls before they get
129127
// into this mapper, so we can safely keep them here.
130-
var objectMapper = (new ObjectMapper())
131-
.configure(SerializationFeature.INDENT_OUTPUT, false)
132-
.setSerializationInclusion(JsonInclude.Include.ALWAYS);
133-
return new JacksonJsonpMapper(objectMapper);
128+
JsonMapper jsonMapper = JsonMapper.builder()
129+
.enable(JsonNodeFeature.WRITE_NULL_PROPERTIES)
130+
.enable(JsonNodeFeature.READ_NULL_PROPERTIES)
131+
.build();
132+
return new Jackson3JsonpMapper(jsonMapper);
134133
}
135134

136135
/**

src/main/java/org/springframework/data/elasticsearch/core/document/Document.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.document;
1717

18-
import java.io.IOException;
18+
import tools.jackson.core.JacksonException;
19+
1920
import java.util.LinkedHashMap;
2021
import java.util.Map;
2122
import java.util.function.Function;
@@ -87,7 +88,7 @@ default Document fromJson(String json) {
8788
clear();
8889
try {
8990
putAll(MapDocument.OBJECT_MAPPER.readerFor(Map.class).readValue(json));
90-
} catch (IOException e) {
91+
} catch (JacksonException e) {
9192
throw new ConversionException("Cannot parse JSON", e);
9293
}
9394
return this;

src/main/java/org/springframework/data/elasticsearch/core/document/MapDocument.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.document;
1717

18+
import tools.jackson.core.JacksonException;
19+
import tools.jackson.databind.ObjectMapper;
20+
1821
import java.util.Collection;
1922
import java.util.LinkedHashMap;
2023
import java.util.Map;
@@ -25,9 +28,6 @@
2528
import org.springframework.data.elasticsearch.support.DefaultStringObjectMap;
2629
import org.springframework.data.mapping.MappingException;
2730

28-
import com.fasterxml.jackson.core.JsonProcessingException;
29-
import com.fasterxml.jackson.databind.ObjectMapper;
30-
3131
/**
3232
* {@link Document} implementation backed by a {@link LinkedHashMap}.
3333
*
@@ -344,7 +344,7 @@ public void forEach(BiConsumer<? super String, ? super Object> action) {
344344
public String toJson() {
345345
try {
346346
return OBJECT_MAPPER.writeValueAsString(this);
347-
} catch (JsonProcessingException e) {
347+
} catch (JacksonException e) {
348348
throw new MappingException("Cannot render document to JSON", e);
349349
}
350350
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
package org.springframework.data.elasticsearch.core.geo;
22

3-
import java.io.IOException;
4-
import com.fasterxml.jackson.core.JsonGenerator;
5-
import com.fasterxml.jackson.core.JsonParser;
6-
import com.fasterxml.jackson.core.Version;
7-
import com.fasterxml.jackson.databind.DeserializationContext;
8-
import com.fasterxml.jackson.databind.JsonDeserializer;
9-
import com.fasterxml.jackson.databind.JsonSerializer;
10-
import com.fasterxml.jackson.databind.SerializerProvider;
11-
import com.fasterxml.jackson.databind.module.SimpleModule;
3+
import tools.jackson.core.JacksonException;
4+
import tools.jackson.core.JsonGenerator;
5+
import tools.jackson.core.JsonParser;
6+
import tools.jackson.databind.DeserializationContext;
7+
import tools.jackson.databind.SerializationContext;
8+
import tools.jackson.databind.ValueDeserializer;
9+
import tools.jackson.databind.ValueSerializer;
10+
1211
import org.springframework.data.geo.Point;
1312

14-
class PointSerializer extends JsonSerializer<Point> {
13+
class PointSerializer extends ValueSerializer<Point> {
1514
@Override
16-
public void serialize(Point value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
17-
gen.writeObject(GeoPoint.fromPoint(value));
15+
public void serialize(Point value, JsonGenerator gen, SerializationContext serializers) throws JacksonException {
16+
gen.writePOJO(GeoPoint.fromPoint(value));
1817
}
1918
}
2019

21-
class PointDeserializer extends JsonDeserializer<Point> {
20+
class PointDeserializer extends ValueDeserializer<Point> {
2221
@Override
23-
public Point deserialize(JsonParser p, DeserializationContext context) throws IOException {
22+
public Point deserialize(JsonParser p, DeserializationContext context) throws JacksonException {
2423
return GeoPoint.toPoint(p.readValueAs(GeoPoint.class));
2524
}
2625
}

src/main/java/org/springframework/data/elasticsearch/core/index/GeoShapeMappingParameters.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.index;
1717

18+
import tools.jackson.databind.node.ObjectNode;
19+
1820
import java.io.IOException;
1921

2022
import org.jspecify.annotations.Nullable;
2123
import org.springframework.data.elasticsearch.annotations.GeoShapeField;
2224
import org.springframework.util.Assert;
2325

24-
import com.fasterxml.jackson.databind.node.ObjectNode;
25-
2626
/**
2727
* @author Peter-Josef Meisch
2828
*/

src/main/java/org/springframework/data/elasticsearch/core/index/MappingBuilder.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
import static org.springframework.data.elasticsearch.core.index.MappingParameters.*;
1919
import static org.springframework.util.StringUtils.*;
2020

21+
import tools.jackson.databind.JsonNode;
22+
import tools.jackson.databind.ObjectMapper;
23+
import tools.jackson.databind.node.ArrayNode;
24+
import tools.jackson.databind.node.ObjectNode;
25+
import tools.jackson.databind.node.StringNode;
26+
import tools.jackson.databind.util.RawValue;
27+
2128
import java.io.IOException;
2229
import java.lang.annotation.Annotation;
2330
import java.nio.charset.Charset;
@@ -48,13 +55,6 @@
4855
import org.springframework.util.StreamUtils;
4956
import org.springframework.util.StringUtils;
5057

51-
import com.fasterxml.jackson.databind.JsonNode;
52-
import com.fasterxml.jackson.databind.ObjectMapper;
53-
import com.fasterxml.jackson.databind.node.ArrayNode;
54-
import com.fasterxml.jackson.databind.node.ObjectNode;
55-
import com.fasterxml.jackson.databind.node.TextNode;
56-
import com.fasterxml.jackson.databind.util.RawValue;
57-
5858
/**
5959
* @author Rizwan Idrees
6060
* @author Mohsin Husen
@@ -184,7 +184,7 @@ protected String buildPropertyMapping(ElasticsearchPersistentEntity<?> entity,
184184
if (!excludeFromSource.isEmpty()) {
185185
ObjectNode sourceNode = objectNode.putObject(SOURCE);
186186
ArrayNode excludes = sourceNode.putArray(SOURCE_EXCLUDES);
187-
excludeFromSource.stream().map(TextNode::new).forEach(excludes::add);
187+
excludeFromSource.stream().map(StringNode::new).forEach(excludes::add);
188188
}
189189

190190
return objectMapper.writer().writeValueAsString(objectNode);
@@ -238,7 +238,7 @@ private void mapEntity(ObjectNode objectNode, @Nullable ElasticsearchPersistentE
238238

239239
if (mappingAnnotation.dynamicDateFormats().length > 0) {
240240
objectNode.putArray(DYNAMIC_DATE_FORMATS).addAll(Arrays.stream(mappingAnnotation.dynamicDateFormats())
241-
.map(TextNode::valueOf).collect(Collectors.toList()));
241+
.map(StringNode::valueOf).collect(Collectors.toList()));
242242
}
243243

244244
if (runtimeFields != null) {
@@ -546,7 +546,7 @@ private void addJoinFieldMapping(ObjectNode propertiesNode, ElasticsearchPersist
546546

547547
if (children.length > 1) {
548548
relationsNode.putArray(parent)
549-
.addAll(Arrays.stream(children).map(TextNode::valueOf).collect(Collectors.toList()));
549+
.addAll(Arrays.stream(children).map(StringNode::valueOf).collect(Collectors.toList()));
550550
} else if (children.length == 1) {
551551
relationsNode.put(parent, children[0]);
552552
}

src/main/java/org/springframework/data/elasticsearch/core/index/MappingParameters.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.index;
1717

18+
import tools.jackson.databind.node.ObjectNode;
19+
import tools.jackson.databind.node.StringNode;
20+
1821
import java.io.IOException;
1922
import java.lang.annotation.Annotation;
2023
import java.util.ArrayList;
@@ -28,9 +31,6 @@
2831
import org.springframework.util.Assert;
2932
import org.springframework.util.StringUtils;
3033

31-
import com.fasterxml.jackson.databind.node.ObjectNode;
32-
import com.fasterxml.jackson.databind.node.TextNode;
33-
3434
/**
3535
* A class to hold the mapping parameters that might be set on
3636
* {@link org.springframework.data.elasticsearch.annotations.Field } or
@@ -285,7 +285,7 @@ public void writeTypeAndParametersTo(ObjectNode objectNode) throws IOException {
285285

286286
if (copyTo != null && copyTo.length > 0) {
287287
objectNode.putArray(FIELD_PARAM_COPY_TO)
288-
.addAll(Arrays.stream(copyTo).map(TextNode::valueOf).collect(Collectors.toList()));
288+
.addAll(Arrays.stream(copyTo).map(StringNode::valueOf).collect(Collectors.toList()));
289289
}
290290

291291
if (ignoreAbove != null) {

0 commit comments

Comments
 (0)