欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 前端技术 > javascript >内容正文

javascript

springbatch 写文件json(jsonfileitemwriter)用法(十二) -凯发ag旗舰厅登录网址下载

发布时间:2025/1/21 javascript 14 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 springbatch 写文件json(jsonfileitemwriter)用法(十二) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

    • 一、抽取写出json文件公共writer
    • 二、processor
    • 三、配置写json文件job
    • 四、执行job

前言:在一些业务场景中,可能需要写出json,来做业务逻辑处理,springbatch已经帮我们封装好了写出json的writer

springbatch其它文章直通车:

  • springbatch读单个文件(flatfileitemreader)和写单个文件(flatfileitemwriter)(一)
  • springbatch顺序读取多文件(multiresourceitemreader)和顺序写文件(multiresourceitemwriter)(二)
  • springbatch读数据库(mybatispagingitemreader)(三)
  • springbatch读文件(flatfileitemreader)写据库(mybatisbatchitemwriter)(四)
  • springbatch 监听器之job监听器(jobexecutionlistener)和step监听器(stepexecutionlistener)(五)
  • springbatch 监听器之chunk监听器(chunklistener)和skip监听器(skiplistener)(六)
  • springbatch 多线程(taskexecutor)启动job详解 (七)
  • springbatch 配置并行启动job详解 (八)
  • springbatch 批处理分区(partitioner )分片(九)
  • springbatch tasklet实现和用法(十)
  • springbatch 读取json(jsonitemreader)用法(十一)

代码已上传github上面地址:https://github.com/fadehub/spring-boot-learn/tree/master/spring-boot-springbatch

一、抽取写出json文件公共writer

该jsonfileitemwriter 将项目的编组委托给 org.springframework.batch.item.json.jsonobjectmarshaller接口。该接口的约定是获取一个对象并将其marshall为json字符串。目前提供两种实现方式:

jackson:org.springframework.batch.item.json.jacksonjsonobjectmarshaller

gsonjson:gsonorg.springframework.batch.item.json.gsonjsonobjectmarshaller

commonjsonitemwriter 继承jsonfileitemwriter

package com.sl.common;import org.springframework.batch.item.json.jsonfileitemwriter; import org.springframework.batch.item.json.jsonobjectmarshaller; import org.springframework.core.io.resource;/*** @author shuliangzhao* @title: commonjsonitemwriter* @projectname spring-boot-learn* @description: todo* @date 2019/9/18 19:54*/ public class commonjsonitemwriter extends jsonfileitemwriter {public commonjsonitemwriter(resource resource, jsonobjectmarshaller jsonobjectmarshaller) {super(resource, jsonobjectmarshaller);setresource(resource);setjsonobjectmarshaller(jsonobjectmarshaller);setencoding("utf-8");setname("tradejsonfileitemwriter");} }

二、processor

package com.sl.processor;import com.sl.common.commonprocessor; import com.sl.entity.cafecat; import com.sl.entity.cat; import org.springframework.batch.core.configuration.annotation.stepscope; import org.springframework.stereotype.component;/*** @author shuliangzhao* @title: catwritejsonprocessor* @projectname spring-boot-learn* @description: todo* @date 2019/9/18 20:00*/ @component @stepscope public class catwritejsonprocessor extends commonprocessor {@overridepublic void processor(cafecat o, cat cat) {o.setcatname(cat.getcatname());o.setcatage(cat.getcatage());o.setcataddress(cat.getcataddress());} }

三、配置写json文件job

package com.sl.config;import com.sl.common.commonfileitemreader; import com.sl.common.commonjsonitemwriter; import com.sl.entity.cafecat; import com.sl.entity.cat; import com.sl.processor.catwritejsonprocessor; import org.springframework.batch.core.job; import org.springframework.batch.core.step; import org.springframework.batch.core.configuration.annotation.enablebatchprocessing; import org.springframework.batch.core.configuration.annotation.jobbuilderfactory; import org.springframework.batch.core.configuration.annotation.stepbuilderfactory; import org.springframework.batch.core.configuration.annotation.stepscope; import org.springframework.batch.item.json.jacksonjsonobjectmarshaller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.core.io.filesystemresource;/*** 读取csv文件,写出json* @author shuliangzhao* @title: catwritejsonconfiguration* @projectname spring-boot-learn* @description: todo* @date 2019/9/18 19:56*/ @configuration @enablebatchprocessing public class catwritejsonconfiguration {@autowiredprivate jobbuilderfactory jobbuilderfactory;@autowiredprivate stepbuilderfactory stepbuilderfactory;@autowiredprivate catwritejsonprocessor catwritejsonprocessor;@beanpublic job catwritejsonjob() {return jobbuilderfactory.get("catwritejsonjob").start(catwritejsonstep()).build();}@beanpublic step catwritejsonstep() {return stepbuilderfactory.get("catwritejsonstep").chunk(10).reader(catreadcsvitemreader()).processor(catwritejsonprocessor).writer(commonjsonitemwriter()).build();}@bean@stepscopepublic commonfileitemreader catreadcsvitemreader() {return new commonfileitemreader<>(cat.class);}@bean@stepscopepublic commonjsonitemwriter commonjsonitemwriter() {filesystemresource filesystemresource = new filesystemresource("d:\\aplus\\shuqian\\source\\" cafecat.class.getsimplename() ".json");return new commonjsonitemwriter(filesystemresource,new jacksonjsonobjectmarshaller());} }

四、执行job

写出json文件:

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的springbatch 写文件json(jsonfileitemwriter)用法(十二)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

网站地图