gpt4 book ai didi

java - Spring 批处理 : Create an ItemReader that reads an xml file from a web service

转载 作者:搜寻专家 更新时间:2023-10-31 20:20:54 32 4
gpt4 key购买 nike

我正在尝试创建一个 Spring Batch 作业,该作业将处理一个将通过 REST 调用提供的 xml 文件。

我正在尝试使用托管在 Internet 上的 XML 文件来对此进行测试。该文件位于:http://www.w3schools.com/xml/plant_catalog.xml

我在本地下载了这个文件并且能够将它转换为一个对象并写入它,但我不知道如果不在本地下载文件我该如何做同样的事情。这在本地有效,但我如何指定一个 URL 作为 xml 文件读取的资源?谢谢:)

启动上下文.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。看看 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" />

关于java - Spring 批处理 : Create an ItemReader that reads an xml file from a web service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19231689/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com