Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch: Create an ItemReader that reads an xml file from a web service

I am trying to create a Spring Batch job that will process an xml file that will be served up through a REST call.

I am trying to use an XML file that's hosted on the internet to test this. The file is located at: http://www.w3schools.com/xml/plant_catalog.xml

I downloaded this file locally and am able to convert it to an object and write it, but I don't know how I can do the same without downloading the file locally. This works locally, but how can I specify a URL as the resource for the xml file to read? Thanks :)

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

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 anyone wants to see the mapping.xml file this is what it looks like. It maps the xml nodes to a domain object called 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>
like image 608
user1521567 Avatar asked Oct 20 '22 23:10

user1521567


1 Answers

The StaxEventItemReader takes a Resource. Take a look at the documentation, you will see that you can easily use a resource that is not on your filesystem. Anything on which you can get an InputStream can probably be converted to a Resource. The default use an UrlResource with a fallback to ClasspathResource as far as I remember.

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.

So you can probably just use this:

<property name="resource" value="http://www.w3schools.com/xml/plant_catalog.xml" />
like image 78
Sebastien Lorber Avatar answered Oct 24 '22 11:10

Sebastien Lorber