无法使用 Spring 批处理将 XML 文件中的 Date 和 BigDecimal 值保存到 Mongo,为什么?

Unable to save Date and BigDecimal values from XML file into Mongo using Spring Batch, Why?

我正在开发 Spring Batch XML to MongoDB 示例。在此示例中,我无法将 DateBigDecimal 值保存到 MongoDB.

Report.java

@XmlRootElement(name="report")
@XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
public class Report implements Serializable{
    private static final long serialVersionUID = 1L;

    private int id;
    private Date date;
    private long impression;
    private int clicks;
    private BigDecimal earning;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public long getImpression() {
        return impression;
    }

    public void setImpression(long impression) {
        this.impression = impression;
    }

    public int getClicks() {
        return clicks;
    }

    public void setClicks(int clicks) {
        this.clicks = clicks;
    }

    public BigDecimal getEarning() {
        return earning;
    }

    public void setEarning(BigDecimal earning) {
        this.earning = earning;
    }

    @Override
    public String toString() {
        SimpleDateFormat dateFormatter = new SimpleDateFormat("M/d/yyyy");
        return "Report [id=" + id + ", date=" + dateFormatter.format(date) + ", impression=" + impression + ", clicks=" + clicks + ", earning="
                + earning + "]";
    }
}

工作-report.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/batch  http://www.springframework.org/schema/batch/spring-batch.xsd
    http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util.xsd">


    <!-- XML Item Reader -->
    <bean id="xmlItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
        <property name="resource" value="classpath:xml/report.xml" />
        <property name="unmarshaller" ref="reportUnMarshaller" />
        <property name="fragmentRootElementName" value="report" />
    </bean>


    <bean id="reportUnMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <value>com.mkyong.model.Report</value>
        </property>
     </bean>



    <!-- Report Unmarshaller -->
    <!-- <bean id="reportUnmarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
        <property name="aliases">
            <util:map id="aliases">
                <entry key="record" value="com.mkyong.model.Report" />

                <entry key="date" value="java.lang.String" />
                <entry key="impression" value="java.lang.Long" />
                <entry key="clicks" value="java.lang.Integer" />
                <entry key="earning" value="java.math.BigDecimal" />

            </util:map>
        </property>

        <property name="converters">
            <array>
                <ref bean="reportConverter" />
            </array>
        </property>
    </bean> -->

    <!-- MongoItemWriter -->
    <bean id="mongodbItemWriter" class="org.springframework.batch.item.data.MongoItemWriter">
        <property name="template" ref="mongoTemplate" />
        <property name="collection" value="report" />
    </bean>

    <!-- <bean id="reportConverter" class="com.mkyong.converter.ReportConverter" /> -->


    <!-- JOB -->
    <batch:job id="reportJob">
        <batch:step id="step1">
            <batch:tasklet>
                <batch:chunk reader="xmlItemReader" writer="mongodbItemWriter" commit-interval="1" />
            </batch:tasklet>
        </batch:step>
    </batch:job>
</beans>

report.xml

<?xml version="1.0" encoding="UTF-8" ?>
<report>
    <record id="1">
        <date>6/1/2013</date>
        <impression>139,237</impression>
        <clicks>40</clicks>
        <earning>220.90</earning>
    </record>
    <record id="2">
        <date>6/2/2013</date>
        <impression>339,100</impression>
        <clicks>60</clicks>
        <earning>320.88</earning>
    </record>
    <record id="3">
        <date>6/3/2013</date>
        <impression>431,436</impression>
        <clicks>76</clicks>
        <earning>270.80</earning>
    </record>
    <record id="4">
        <date>3/12/2016</date>
        <impression>534,987</impression>
        <clicks>43</clicks>
        <earning>454.80</earning>
    </record>
</report>

不能 能够将 Date 和 BigDecimal 值保存到 MongoDB 中:我还看到只有一个 record/document 被存储到数据库,为什么?

database.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/data/mongo
        http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

        <!-- connect to mongodb -->
    <mongo:mongo host="127.0.0.1" port="27017" />
    <mongo:db-factory dbname="yourdb" />

    <bean id="mongoTemplate" 
                class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>
</beans>

context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <!-- stored job-meta in memory --> 
    <bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    <property name="transactionManager" ref="transactionManager" />
    </bean>

    <bean id="transactionManager"
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />


    <bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
    </bean>
</beans>

pom.xml:

<properties>

        <!-- Generic properties -->
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <spring.version>3.2.2.RELEASE</spring.version>
        <spring.batch.version>2.2.0.RELEASE</spring.batch.version>
        <spring.data.version>1.2.1.RELEASE</spring.data.version>
        <mongodb.driver.version>3.0.4</mongodb.driver.version>

        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>
        <jcl.slf4j.version>1.7.12</jcl.slf4j.version>

        <!-- Test -->
        <junit.version>4.11</junit.version>

    </properties>

    <dependencies>
        <!-- Spring Core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring XML to/back object -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring Batch dependencies -->
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>${spring.batch.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-infrastructure</artifactId>
            <version>${spring.batch.version}</version>
        </dependency>

        <!-- Spring Batch unit test -->
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <version>${spring.batch.version}</version>
        </dependency>

        <!-- MongoDB database driver -->
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>${mongodb.driver.version}</version>
        </dependency>

        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${jcl.slf4j.version}</version>
        </dependency>

        <!-- Spring data mongodb -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>${spring.data.version}</version>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!-- Testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.5</version>
            <scope>test</scope>
        </dependency>

我记得几年前遇到过这个问题,我试图在 jsp 文件中映射一个 BigDecimal 字段。
为了解决这个异常,我创建了一个转换器 class。不确定是否有更好的方法来做到这一点。下面的代码只是我所做的一个例子。

public class BigDecimalConverter extends StrutsTypeConverter{


 @Override

 public Object convertFromString(Map context, String[] values, Class toClass) {

  if(values!=null && values.length>0){

   String s=values[0];

   if(s!=null && s.trim().length()>0){

   BigDecimal bd = new BigDecimal(s);

   return bd.setScale(2, BigDecimal.ROUND_HALF_UP);

   }

  }

  return null;

 }

 

 @Override

 public String convertToString(Map context, Object o) {

  return o.toString();

  

 }

不确定当前 maven dependencies 导致问题的原因。您只需相信将 maven dependencies 更新到它的最新版本,问题就会自动解决。我刚刚测试了它,我发现它工作正常。结果分享如下:

<spring.version>4.2.4.RELEASE</spring.version>
<spring.batch.version>3.0.6.RELEASE</spring.batch.version>
<spring.data.version>1.8.4.RELEASE</spring.data.version>
<mongodb.driver.version>3.1.1</mongodb.driver.version>