Class JsonItemReader<T>
java.lang.Object
org.springframework.batch.infrastructure.item.ItemStreamSupport
org.springframework.batch.infrastructure.item.support.AbstractItemStreamItemReader<T>
org.springframework.batch.infrastructure.item.support.AbstractItemCountingItemStreamItemReader<T>
org.springframework.batch.infrastructure.item.json.JsonItemReader<T>
- Type Parameters:
T- the type of json objects to read
- All Implemented Interfaces:
ResourceAwareItemReaderItemStream<T>,ItemReader<T>,ItemStream,ItemStreamReader<T>,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware
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
ConstructorsConstructorDescriptionJsonItemReader(org.springframework.core.io.Resource resource, JsonObjectReader<T> jsonObjectReader) Create a newJsonItemReaderinstance. -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoClose()Close the resources opened inAbstractItemCountingItemStreamItemReader.doOpen().protected voiddoOpen()Open resources necessary to start reading input.protected @Nullable TdoRead()Read next item from input.protected voidjumpToItem(int itemIndex) Move to the given item index.voidsetJsonObjectReader(JsonObjectReader<T> jsonObjectReader) Set theJsonObjectReaderto use to read and map Json fragments to domain objects.voidsetResource(org.springframework.core.io.Resource resource) voidsetStrict(boolean strict) In strict mode the reader will throw an exception onAbstractItemCountingItemStreamItemReader.open(ExecutionContext)if the input resource does not exist.Methods inherited from class org.springframework.batch.infrastructure.item.support.AbstractItemCountingItemStreamItemReader
close, getCurrentItemCount, isSaveState, open, read, setCurrentItemCount, setMaxItemCount, setSaveState, updateMethods inherited from class org.springframework.batch.infrastructure.item.ItemStreamSupport
getExecutionContextKey, getName, setBeanName, setExecutionContextName, setNameMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.batch.infrastructure.item.ItemReader
readMethods inherited from interface org.springframework.batch.infrastructure.item.ItemStream
close, open, update
-
Constructor Details
-
JsonItemReader
public JsonItemReader(org.springframework.core.io.Resource resource, JsonObjectReader<T> jsonObjectReader) Create a newJsonItemReaderinstance.- Parameters:
resource- the input json resourcejsonObjectReader- the json object reader to use
-
-
Method Details
-
setJsonObjectReader
Set theJsonObjectReaderto 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(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:
setResourcein interfaceResourceAwareItemReaderItemStream<T>
-
doRead
Description copied from class:AbstractItemCountingItemStreamItemReaderRead next item from input.- Specified by:
doReadin classAbstractItemCountingItemStreamItemReader<T>- Returns:
- an item or
nullif the data source is exhausted - Throws:
Exception- Allows subclasses to throw checked exceptions for interpretation by the framework
-
doOpen
Description copied from class:AbstractItemCountingItemStreamItemReaderOpen resources necessary to start reading input.- Specified by:
doOpenin classAbstractItemCountingItemStreamItemReader<T>- Throws:
Exception- Allows subclasses to throw checked exceptions for interpretation by the framework
-
doClose
Description copied from class:AbstractItemCountingItemStreamItemReaderClose the resources opened inAbstractItemCountingItemStreamItemReader.doOpen().- Specified by:
doClosein classAbstractItemCountingItemStreamItemReader<T>- Throws:
Exception- Allows subclasses to throw checked exceptions for interpretation by the framework
-
jumpToItem
Description copied from class:AbstractItemCountingItemStreamItemReaderMove 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:
jumpToItemin 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
-