保存Mongo Pojo时@DateTimeFormat不是Woking

时间:2016-12-17 09:47:39

标签: spring mongodb spring-data datetime-format

我正在与@DateTimeFormat斗争     我只想在Mongo中保存日期,所以我正在使用     @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)     但是当对象在mongo中保存时,它会节省2016-12-17 09:30:19.795Z 我使用spring 4.1.1和spring-data-mongodb 1.7.2 我的module-context.xml是

<context:component-scan base-package="psl.service.core.agentanalytics.internal"/>

<jpa:repositories base-package="psl.service.core.agentanalytics.internal.repository"
                  repository-impl-postfix="DetectPostfixToken"
                  entity-manager-factory-ref="entityManagerFactory"/>

<bean name="/agentanalytics/public" parent="contextAwareServiceExporter"
      p:service-ref="agentAnalyticsServicePublicImpl"
      p:serviceInterface="psl.service.core.agentanalytics.api.AgentAnalyticsServicePublic"/>

<bean name="/agentanalytics/jobs/public" parent="contextAwareServiceExporter"
      p:service-ref="agentAnalyticsServiceJobsPublicImpl"
      p:serviceInterface="psl.service.core.agentanalytics.api.AgentAnalyticsServiceJobsPublic"/>

<tx:annotation-driven/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg ref="mongo"/>
    <constructor-arg name="databaseName" value="xyz"/>
</bean>
<bean class=
              "org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<mongo:repositories
        base-package="psl.service.core.agentanalytics.internal" mongo-template-ref="mongoTemplate"/>


<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
      p:entityManagerFactory-ref="entityManagerFactory"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter" p:persistenceUnitName="agentanalyticsPU">
    <property name="packagesToScan">
        <value>psl.service.core.agentanalytics.internal</value>
    </property>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
    </property>
</bean>

<bean id="mapper" class="psl.shared.util.dozerextn.internal.DozerBeanMapperFactoryBean">
    <property name="mappingFiles">
        <list>
            <value>classpath:META-INF/spring/dozer-mapping.xml</value>
        </list>
    </property>
</bean>

和我的mongo pojo

import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.format.annotation.DateTimeFormat;
import psl.service.core.agentanalytics.api.AgentDataPoints;
import psl.shared.impl.entity.MongoKeyedEntity;

import java.io.Serializable;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by agent1 on 14/12/16.
 */

@CompoundIndex(name = "account_date_idx", def = "{'account' : 1, 'date' : 1}", unique = true)
@Document(collection = "agent_data_storage")
public class AgentDataStorage extends MongoKeyedEntity<String> implements Serializable {
    @Field
    private Long account;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    @Field()
    private Date date;

    @Field
    private Map<String, Integer> dataPoints = new HashMap<>();

    public AgentDataStorage(Long account) {
        this.account = account;
        this.date = new Date();
        for (AgentDataPoints dataPoint : EnumSet.allOf(AgentDataPoints.class)) {
            this.dataPoints.put(dataPoint.toString(), 0);
        }
    }

    public AgentDataStorage(String account) {
        this.account = Long.valueOf(account);
        for (AgentDataPoints dataPoint : EnumSet.allOf(AgentDataPoints.class)) {
            this.dataPoints.put(dataPoint.toString(), 0);
        }
    }

    public AgentDataStorage(Long account, Date date) {
        this.account = account;
        this.date = date;
        for (AgentDataPoints dataPoint : EnumSet.allOf(AgentDataPoints.class)) {
            this.dataPoints.put(dataPoint.toString(), 0);
        }
    }

    public AgentDataStorage(Long account, Date date, Map<String, Integer> dataPoints) {
        this.account = account;
        this.date = date;
        this.dataPoints = dataPoints;
    }

    public AgentDataStorage(String account, Date date) {
        this.account = Long.valueOf(account);
        this.date = date;
        for (AgentDataPoints dataPoint : EnumSet.allOf(AgentDataPoints.class)) {
            this.dataPoints.put(dataPoint.toString(), 0);
        }
    }

    public Long getAccount() {
        return account;
    }

    public void setAccount(Long account) {
        this.account = account;
    }

    public Date getDate() {
        return date;
    }

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

    public Map<String, Integer> getDataPoints() {
        return dataPoints;
    }

    public void setDataPoints(Map<String, Integer> dataPoints) {
        this.dataPoints = dataPoints;
    }

    public void updateDataPoint(AgentDataPoints agentDataPoints, Integer value) {
        this.dataPoints.put(String.valueOf(agentDataPoints), value);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("AgentDataStorage{");
        sb.append("account=").append(account);
        sb.append(", date=").append(date);
        sb.append(", dataPoints=").append(dataPoints);
        sb.append('}');
        return sb.toString();
    }
}

0 个答案:

没有答案