Skip to content

Commit 61286ec

Browse files
banseok1216fmbenhassine
authored andcommitted
Avoid ByteArrayResource fallback when JsonItemReaderBuilder resource is null
Resolves #5234 Signed-off-by: banseok1216 <bansuk1216@naver.com>
1 parent 6549c19 commit 61286ec

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2025 the original author or authors.
2+
* Copyright 2018-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,12 +55,22 @@ public class JsonItemReader<T> extends AbstractItemCountingItemStreamItemReader<
5555

5656
private static final Log LOGGER = LogFactory.getLog(JsonItemReader.class);
5757

58-
private Resource resource;
58+
private @Nullable Resource resource;
5959

6060
private JsonObjectReader<T> jsonObjectReader;
6161

6262
private boolean strict = true;
6363

64+
/**
65+
* Create a new {@link JsonItemReader} instance.
66+
* @param jsonObjectReader the json object reader to use
67+
* @since 6.0.4
68+
*/
69+
public JsonItemReader(JsonObjectReader<T> jsonObjectReader) {
70+
Assert.notNull(jsonObjectReader, "The json object reader must not be null.");
71+
this.jsonObjectReader = jsonObjectReader;
72+
}
73+
6474
/**
6575
* Create a new {@link JsonItemReader} instance.
6676
* @param resource the input json resource
@@ -92,7 +102,7 @@ public void setStrict(boolean strict) {
92102
}
93103

94104
@Override
95-
public void setResource(Resource resource) {
105+
public void setResource(@Nullable Resource resource) {
96106
this.resource = resource;
97107
}
98108

@@ -103,6 +113,8 @@ public void setResource(Resource resource) {
103113

104114
@Override
105115
protected void doOpen() throws Exception {
116+
Assert.notNull(this.resource, "The resource must not be null.");
117+
106118
if (!this.resource.exists()) {
107119
if (this.strict) {
108120
throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode)");

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2025 the original author or authors.
2+
* Copyright 2018-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@
2626
import org.springframework.batch.infrastructure.item.support.AbstractItemCountingItemStreamItemReader;
2727
import org.springframework.batch.infrastructure.item.json.JsonItemReader;
2828
import org.springframework.batch.infrastructure.item.json.JsonObjectReader;
29-
import org.springframework.core.io.ByteArrayResource;
3029
import org.springframework.core.io.Resource;
3130
import org.springframework.util.Assert;
3231
import org.springframework.util.StringUtils;
@@ -156,11 +155,10 @@ public JsonItemReader<T> build() {
156155
if (this.resource == null) {
157156
logger.debug("The resource is null. This is only a valid scenario when "
158157
+ "injecting it later as in when using the MultiResourceItemReader");
159-
// TODO check if this is feasible
160-
this.resource = new ByteArrayResource(new byte[0]);
161158
}
162-
JsonItemReader<T> reader = new JsonItemReader<>(this.resource, this.jsonObjectReader);
163-
reader.setJsonObjectReader(this.jsonObjectReader);
159+
JsonItemReader<T> reader = new JsonItemReader<>(this.jsonObjectReader);
160+
reader.setResource(this.resource);
161+
164162
if (this.name != null) {
165163
reader.setName(this.name);
166164
}

0 commit comments

Comments
 (0)