![](/img/trans.png)
[英]Spring Batch: MultiResourceItemReader with custom ItemReader reads the same file ad infinitum
[英]Spring Batch: Create an ItemReader that reads an xml file from a web service
我正在嘗試創建一個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
獲取Resource
。 看一下文檔 ,您將看到您可以輕松使用不在您的文件系統上的資源。 您可以獲得InputStream的任何內容都可以轉換為Resource。 默認情況下,我記得使用UrlResource
並返回ClasspathResource
。
4.3.1。 UrlResource對象
UrlResource包裝java.net.URL,可用於訪問通常可通過URL訪問的任何對象,例如文件,HTTP目標,FTP目標等。所有URL都具有標准化的字符串表示形式,例如適當的標准化前綴用於表示另一個URL類型。 這包括文件:用於訪問文件系統路徑,http:用於通過HTTP協議訪問資源,ftp:用於通過FTP訪問資源等.UrlResource由Java代碼使用UrlResource構造函數顯式創建,但通常在您使用時隱式創建調用一個API方法,該方法接受一個表示路徑的String參數。 對於后一種情況,JavaBeans PropertyEditor最終將決定要創建哪種類型的Resource。 如果路徑字符串包含一些眾所周知的(對於它)前綴,例如classpath:,它將為該前綴創建適當的專用資源。 但是,如果它無法識別前綴,則會假設這只是一個標准的URL字符串,並將創建一個UrlResource。
所以你可以使用這個:
<property name="resource" value="http://www.w3schools.com/xml/plant_catalog.xml" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.