Java – spring batch: create an itemreader that reads XML files from a web service

I am trying to create a spring batch job that will process the XML file that will be provided through the rest call

I'm trying to test it with an XML file hosted on the Internet The file is located at: http://www.w3schools.com/xml/plant_catalog.xml

I downloaded this file locally and was able to convert it into an object and write it, but I don't know how to do this without downloading the file locally This works locally, but how do I specify a URL as a resource for the XML file to read? Thank you:)

Context after launch 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>

Custom reader 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>

For reference, if someone wants to see mapping XML file, that's what it looks like It maps the XML node to a node named plant Java domain object

<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>

Solution

Staxeventitemreader gets the resource

So you can use this:

<property name="resource" value="http://www.w3schools.com/xml/plant_catalog.xml" />
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>