【发布时间】:2015-02-17 14:19:54
【问题描述】:
我有一个复杂的 json 文件(带有嵌套的 json 数组)结构,如下所示:
{"persons":[
{"id":"1", "firstName": "X", "lastName": "X", "infos": [{"address":[{"city": "X", "country": "X"}]}]},
{"id":"2", "firstName": "Y", "lastName": "Y", "infos": [{"address":[{"city": "Y", "country": "Y"}]}]}
]}
我想单独阅读每一行(一个人)
所以我的spring批处理配置是这样的
<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
<property name="resource" value="#{jobParameters[file]}" />
<property name="recordSeparatorPolicy" ref="recordPolicy" />
<property name="lineMapper" ref="lineMapper" />
</bean>
<bean id="lineMapper"
class="com.batchs.personJob.PersonLineMapper">
<property name="delegate" ref="lineMapperType" />
</bean>
<bean id="lineMapperType"
class="org.springframework.batch.item.file.mapping.JsonLineMapper" />
<bean id="recordPolicy"
class="org.springframework.batch.item.file.separator.JsonRecordSeparatorPolicy" />
映射器类看起来像
public class PersonLineMapper implements LineMapper<Person> {
private JsonLineMapper delegate;
public mapLine(String line, int lineNumber) throws Exception {
Map<String, Object> personAsMap = delegate.mapLine(line, lineNumber);
Person person = new Person();
// map fields
return person ;
}
public void setDelegate(JsonLineMapper delegate) {
this.delegate = delegate;
}
}
问题在于读者只读取一行(所以一次提交),因为他像一整行一样读取我的 json 文件中的人员数组,但我希望每行读取一行(一次一个人)
如何做到这一点?
我尝试过使用这样的简单 json 文件:
{ "id": "1",
"firstName": "X",
"lastName": "X"}
{ "id": "2",
"firstName": "Y",
"lastName": "Y"}
而且效果很好……我一个一个地读了每个人
非常感谢
【问题讨论】:
标签: java json spring spring-batch