spring - SpringBatch - how to set up via java config the JsonLineMapper for reading a simple json file -
how change "setlinetokenizer(new delimitedlinetokenizer()...)" "jsonlinemapper" in first code below? basicaly, working csv want change read simple json file. found threads here asking complex json not case. firstly thought should use diferent approach csv way, after read sbiach05sample.pdf (see link , snippet @ bottom), understood flatfileitemreader can used read json format. in almost similiar question, can guess not in wrong direction. please, trying find simplest elegant , recommended way fixing snippet code. so, wrapper below, unless obligated work way, seems go further. additionally, wrapper seems me more java 6 style tentative takes advantage of anonimous method java 7 (as far can judge studies). please, advise higly appreciated.
//my code
@bean @stepscope public flatfileitemreader<message> reader() { log.info("itemreader >>"); flatfileitemreader<message> reader = new flatfileitemreader<message>(); reader.setresource(new classpathresource("test_json.js")); reader.setlinemapper(new defaultlinemapper<message>() { { setlinetokenizer(new delimitedlinetokenizer() { { setnames(new string[] { "field1", "field2"...
//sample using wrapper
http://www.manning.com/templier/sbiach05sample.pdf import org.springframework.batch.item.file.linemapper; import org.springframework.batch.item.file.mapping.jsonlinemapper; import com.manning.sbia.ch05.product; public class wrappedjsonlinemapper implements linemapper<product> { private jsonlinemapper delegate; public product mapline(string line, int linenumber) throws exception { map<string,object> productasmap = delegate.mapline(line, linenumber); product product = new product(); product.setid((string)productasmap.get("id")); product.setname((string)productasmap.get("name")); product.setdescription((string)productasmap.get("description")); product.setprice(new float((double)productasmap.get("price"))); return product; } public void setdelegate(jsonlinemapper delegate) { this.delegate = delegate; } }
really have 2 options parsing json within spring batch job:
- don't create
linemapper
, createlinetokenizer
. spring batch'sdefaultlinemapper
breaks parsing of record 2 phases, parsing record , mapping result object. fact incoming data json vs csv impacts parsing piece (which handledlinetokenizer
). being said, you'd have write ownlinetokenizer
parse jsonfieldset
. - use provided
jsonlinemapper
. spring batch provideslinemapper
implementation uses jackson deserialize json objects java objects.
in either case, can't map linemapper
linetokenizer
accomplish 2 different things.
Comments
Post a Comment