Skip to content

Commit c534e6c

Browse files
committed
Update default configuration of Jackson 3 mapper in JacksonJsonObjectReader
The default value of DeserializationFeature.FAIL_ON_TRAILING_TOKENS has changed from false to true in Jackson 3. This change requires users to disable that feature to be able to use Spring Batch's JsonItemReader correctly. This commit disables that feature by default in JacksonJsonObjectReader so that Spring Batch users do not have to do it manually. Resolves #5047
1 parent c1ec7cc commit c534e6c

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/json/JacksonJsonObjectReader.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import tools.jackson.core.JacksonException;
2222
import tools.jackson.core.JsonParser;
2323
import tools.jackson.core.JsonToken;
24+
import tools.jackson.databind.DeserializationFeature;
2425
import tools.jackson.databind.json.JsonMapper;
2526
import org.jspecify.annotations.Nullable;
2627

@@ -47,13 +48,26 @@ public class JacksonJsonObjectReader<T> implements JsonObjectReader<T> {
4748
private @Nullable InputStream inputStream;
4849

4950
/**
50-
* Create a new {@link JacksonJsonObjectReader} instance.
51+
* Create a new {@link JacksonJsonObjectReader} instance. This will initialize the
52+
* reader with a default {@link JsonMapper} having
53+
* {@link DeserializationFeature#FAIL_ON_TRAILING_TOKENS} and
54+
* {@link DeserializationFeature#FAIL_ON_NULL_FOR_PRIMITIVES} disabled by default. If
55+
* you want to customize the mapper, use
56+
* {@link #JacksonJsonObjectReader(JsonMapper, Class)}.
5157
* @param itemType the target item type
5258
*/
5359
public JacksonJsonObjectReader(Class<? extends T> itemType) {
54-
this(new JsonMapper(), itemType);
60+
this(JsonMapper.builder()
61+
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
62+
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
63+
.build(), itemType);
5564
}
5665

66+
/**
67+
* Create a new {@link JacksonJsonObjectReader} instance.
68+
* @param mapper the json mapper to use
69+
* @param itemType the target item type
70+
*/
5771
public JacksonJsonObjectReader(JsonMapper mapper, Class<? extends T> itemType) {
5872
this.mapper = mapper;
5973
this.itemType = itemType;

spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/json/JacksonJsonItemReaderCommonTests.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package org.springframework.batch.infrastructure.item.json;
1818

19-
import tools.jackson.databind.DeserializationFeature;
20-
import tools.jackson.databind.json.JsonMapper;
21-
2219
import org.springframework.batch.infrastructure.item.sample.Foo;
2320

2421
/**
@@ -28,11 +25,7 @@ class JacksonJsonItemReaderCommonTests extends JsonItemReaderCommonTests {
2825

2926
@Override
3027
protected JsonObjectReader<Foo> getJsonObjectReader() {
31-
JsonMapper jsonMapper = JsonMapper.builder()
32-
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
33-
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
34-
.build();
35-
return new JacksonJsonObjectReader<>(jsonMapper, Foo.class);
28+
return new JacksonJsonObjectReader<>(Foo.class);
3629
}
3730

3831
}

spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/json/JacksonJsonItemReaderFunctionalTests.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package org.springframework.batch.infrastructure.item.json;
1818

1919
import tools.jackson.core.JacksonException;
20-
import tools.jackson.databind.DeserializationFeature;
21-
import tools.jackson.databind.json.JsonMapper;
2220

2321
import org.springframework.batch.infrastructure.item.json.domain.Trade;
2422

@@ -29,11 +27,7 @@ class JacksonJsonItemReaderFunctionalTests extends JsonItemReaderFunctionalTests
2927

3028
@Override
3129
protected JsonObjectReader<Trade> getJsonObjectReader() {
32-
JsonMapper jsonMapper = JsonMapper.builder()
33-
.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
34-
.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
35-
.build();
36-
return new JacksonJsonObjectReader<>(jsonMapper, Trade.class);
30+
return new JacksonJsonObjectReader<>(Trade.class);
3731
}
3832

3933
@Override

0 commit comments

Comments
 (0)