加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Spring Batch:创建一个从Web服务读取xml文件的ItemRead

发布时间:2020-12-14 19:36:52 所属栏目:Java 来源:网络整理
导读:我正在尝试创建一个 Spring Batch作业,该作业将处理将通过REST调用提供的xml文件. 我正在尝试使用托管在互联网上的XML文件来测试它.该文件位于:http://www.w3schools.com/xml/plant_catalog.xml 我在本地下载了这个文件,并且能够将它转换为一个对象并编写它
我正在尝试创建一个 Spring Batch作业,该作业将处理将通过REST调用提供的xml文件.

我正在尝试使用托管在互联网上的XML文件来测试它.该文件位于:http://www.w3schools.com/xml/plant_catalog.xml

我在本地下载了这个文件,并且能够将它转换为一个对象并编写它,但我不知道如何在不在本地下载文件的情况下也能这样做.这在本地工作,但是如何指定URL作为要读取的xml文件的资源?谢谢 :)

发射后的context.xml

<batch:job id="job1">
    <batch:step id="step1">
        <batch:tasklet transaction-manager="transactionManager" start-limit="100" >
            <batch:chunk reader="CustomPlantReader" writer="writer" commit-interval="1" />
        </batch:tasklet>
    </batch:step>
</batch:job>

自定义读者bean:

<bean id="CustomPlantReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step">
    <property name="fragmentRootElementName" value="PLANT" />
    <property name="resource" value="file:/C:/source/plant_catalog.xml" />
    <property name="unmarshaller" ref="PlantUnmarshaller" />
</bean>

<bean id="PlantUnmarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
    <property name="ignoreExtraElements" value="true" />
    <property name="mappingLocation" value="linemapper/mapping.xml" />
</bean>

作为参考,如果有人想看到mapping.xml文件,这就是它的样子.它将xml节点映射到名为Plant.java的域对象

<mapping>
<class name="com.example.project.Plant">
    <map-to xml="PLANT" />

    <field name="common" type="string">
        <bind-xml name="COMMON" node="element"/>
    </field>
    <field name="botanical" type="string">
        <bind-xml name="BOTANICAL" node="element"/>
    </field>
    <field name="zone" type="string">
        <bind-xml name="ZONE" node="element"/>
    </field>
    <field name="light" type="string">
        <bind-xml name="LIGHT" node="element"/>
    </field>
    <field name="price" type="string">
        <bind-xml name="PRICE" node="element"/>
    </field>
    <field name="availability" type="string">
        <bind-xml name="AVAILABILITY" node="element"/>
    </field>

</class></mapping>

解决方法

StaxEventItemReader获取资源.
看一下 documentation,您将看到您可以轻松使用不在您的文件系统上的资源.您可以获得InputStream的任何内容都可以转换为Resource.
默认情况下,我记得使用UrlResource并返回ClasspathResource.

4.3.1. UrlResource

The UrlResource wraps a java.net.URL,and may be used to access any object that is normally accessible via a URL,such as files,an
HTTP target,an FTP target,etc. All URLs have a standardized String
representation,such that appropriate standardized prefixes are used
to indicate one URL type from another. This includes file: for
accessing filesystem paths,http: for accessing resources via the HTTP
protocol,ftp: for accessing resources via FTP,etc.
A UrlResource is created by Java code explicitly using the UrlResource constructor,but will often be created implicitly when you
call an API method which takes a String argument which is meant to
represent a path. For the latter case,a JavaBeans PropertyEditor will
ultimately decide which type of Resource to create. If the path string
contains a few well-known (to it,that is) prefixes such as
classpath:,it will create an appropriate specialized Resource for
that prefix. However,if it doesn’t recognize the prefix,it will
assume the this is just a standard URL string,and will create a
UrlResource.

所以你可以使用这个:

<property name="resource" value="http://www.w3schools.com/xml/plant_catalog.xml" />

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读