Class JsonItemReader<T>
java.lang.Object
org.springframework.batch.item.ItemStreamSupport
org.springframework.batch.item.support.AbstractItemStreamItemReader<T>
org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
org.springframework.batch.item.json.JsonItemReader<T>
- Type Parameters:
T
- the type of json objects to read
- All Implemented Interfaces:
ResourceAwareItemReaderItemStream<T>
,ItemReader<T>
,ItemStream
,ItemStreamReader<T>
public class JsonItemReader<T>
extends AbstractItemCountingItemStreamItemReader<T>
implements ResourceAwareItemReaderItemStream<T>
ItemStreamReader
implementation that reads Json objects from a Resource
having the following format:
[
{
// JSON object
},
{
// JSON object
}
]
The implementation is not thread-safe.
- Since:
- 4.1
- Author:
- Mahmoud Ben Hassine, Jimmy Praet
-
Constructor Summary
ConstructorDescriptionCreate a newJsonItemReader
instance.JsonItemReader
(org.springframework.core.io.Resource resource, JsonObjectReader<T> jsonObjectReader) Create a newJsonItemReader
instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
doClose()
Close the resources opened inAbstractItemCountingItemStreamItemReader.doOpen()
.protected void
doOpen()
Open resources necessary to start reading input.protected T
doRead()
Read next item from input.protected void
jumpToItem
(int itemIndex) Move to the given item index.void
setJsonObjectReader
(JsonObjectReader<T> jsonObjectReader) Set theJsonObjectReader
to use to read and map Json fragments to domain objects.void
setResource
(org.springframework.core.io.Resource resource) void
setStrict
(boolean strict) In strict mode the reader will throw an exception onAbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext)
if the input resource does not exist.Methods inherited from class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
close, getCurrentItemCount, isSaveState, open, read, setCurrentItemCount, setMaxItemCount, setSaveState, update
Methods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, getName, setExecutionContextName, setName
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.batch.item.ItemReader
read
Methods inherited from interface org.springframework.batch.item.ItemStream
close, open, update
-
Constructor Details
-
JsonItemReader
public JsonItemReader(org.springframework.core.io.Resource resource, JsonObjectReader<T> jsonObjectReader) Create a newJsonItemReader
instance.- Parameters:
resource
- the input json resourcejsonObjectReader
- the json object reader to use
-
JsonItemReader
public JsonItemReader()Create a newJsonItemReader
instance.
-
-
Method Details
-
setJsonObjectReader
Set theJsonObjectReader
to use to read and map Json fragments to domain objects.- Parameters:
jsonObjectReader
- the json object reader to use
-
setStrict
public void setStrict(boolean strict) In strict mode the reader will throw an exception onAbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext)
if the input resource does not exist.- Parameters:
strict
- true by default
-
setResource
public void setResource(org.springframework.core.io.Resource resource) - Specified by:
setResource
in interfaceResourceAwareItemReaderItemStream<T>
-
doRead
Description copied from class:AbstractItemCountingItemStreamItemReader
Read next item from input.- Specified by:
doRead
in classAbstractItemCountingItemStreamItemReader<T>
- Returns:
- an item or
null
if the data source is exhausted - Throws:
Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework
-
doOpen
Description copied from class:AbstractItemCountingItemStreamItemReader
Open resources necessary to start reading input.- Specified by:
doOpen
in classAbstractItemCountingItemStreamItemReader<T>
- Throws:
Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework
-
doClose
Description copied from class:AbstractItemCountingItemStreamItemReader
Close the resources opened inAbstractItemCountingItemStreamItemReader.doOpen()
.- Specified by:
doClose
in classAbstractItemCountingItemStreamItemReader<T>
- Throws:
Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework
-
jumpToItem
Description copied from class:AbstractItemCountingItemStreamItemReader
Move to the given item index. Subclasses should override this method if there is a more efficient way of moving to given index than re-reading the input usingAbstractItemCountingItemStreamItemReader.doRead()
.- Overrides:
jumpToItem
in classAbstractItemCountingItemStreamItemReader<T>
- Parameters:
itemIndex
- index of item (0 based) to jump to.- Throws:
Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework
-