spring-projects / spring-projects/spring-batch
Support Skip Functionality on ParseException in JacksonJsonObjectReader
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3k
- Forks
- 2.5k
- Avg merge
- 6d 53m
- Merged PRs (30d)
- 3
Description
Spring batch version : 5.1.2
Java version : 21
First, I apologize for posting this message in English, as I am using ChatGPT for translation due to my limited English proficiency.
I have been reviewing the skip feature in Spring Batch.
I implemented the reader as a JsonItemReader and used JacksonJsonObjectReader as the jsonObjectReader.
To intentionally trigger a ParseException, I set the age field as a string, as shown below.
{
"name": "mgpark",
"email": "david.taylor@example.com",
"age": "34a",
"city": "San Francisco"
},
However, rather than skipping the error as expected, the process terminated at this point.
Upon further investigation, I observed that in JacksonJsonObjectReader.read(), an IOException results in a ParseException being thrown. However, the jsonParser token remains at the error position rather than moving to the next object. Consequently, when attempting to read the following item, jsonParser.nextToken() returns FIELD_NAME, which results in null being returned, thereby terminating the process prematurely even though there are more items to read.
To ensure that the skip functionality works correctly, it seems necessary to add a mechanism that advances the token to the next object in the event of an error.
@Nullable
public T read() throws Exception {
try {
return this.jsonParser.nextToken() == JsonToken.START_OBJECT ? this.mapper.readValue(this.jsonParser, this.itemType) : null;
} catch (IOException var2) {
IOException e = var2;
throw new ParseException("Unable to read next JSON object", e);
}
}
For reference, the job configuration I used can be found here:
RestartJob.java on GitHub
Thank you very much for your attention to this matter.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with JacksonJsonObjectReader.read() and the supplied RestartJob.java configuration, then trace how JsonItemReader handles the ParseException and resumes reading. Reproduce the malformed age value followed by another JSON object; done means the malformed item can be skipped and subsequent objects are still processed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100