1
votes

I know, that here are plenty examples with JSON-date format on the client side from Spring MVC, but in my case everything works fine, except one specific case with composite contract.

The problem is that on the client side JSON response side in the curObj the date currencyOpenedDate received in the string format, at the same time in the depObj.subdivisionClosedDate date is specified in the timestamp format. Screen of the response you can find at the bottom of the question.

The question is how to receive curObj.currencyOpenedDate in the timestamp format?

Spring configuration

<context:component-scan base-package="ru.iteco.kursval.server.rest"/>
<context:annotation-config/>
<!-- Configures the @Controller programming model -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </list>
    </property>
</bean>
<!-- Total customization - see below for explanation. -->
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false"/>
    <property name="favorParameter" value="true"/>
    <property name="parameterName" value="mediaType"/>
    <property name="ignoreAcceptHeader" value="true"/>
    <property name="useJaf" value="false"/>
    <property name="defaultContentType" value="application/json"/>

    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json"/>
            <entry key="xml" value="application/xml"/>
        </map>
    </property>
</bean>

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean:bean class="ru.iteco.kursval.server.rest.config.ApiInterceptor" autowire="constructor">
        </bean:bean>
    </mvc:interceptor>
</mvc:interceptors>

<!-- Make this available across all of Spring MVC -->
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>

Composite

public class RegionalBankBlockCurrencyDto implements Serializable {
    //.....
    private Long curId;
    /*----> here */ private CurrencyDto curObj;
    private Long depId;
    private SubdivisionDto depObj;

    // .... getters and setters

}

Currency object

public class CurrencyDto implements Serializable {

    private Date currencyOpenedDate;
    private Date currencyClosedDate;

    public Date getCurrencyOpenedDate() {
        return currencyOpenedDate;
    }

    public void setCurrencyOpenedDate(Date currencyOpenedDate) {
        this.currencyOpenedDate = currencyOpenedDate;
    }

    public Date getCurrencyClosedDate() {
        return currencyClosedDate;
    }

    public void setCurrencyClosedDate(Date currencyClosedDate) {
        this.currencyClosedDate = currencyClosedDate;
    }

}

Currency and Subdivision DTOs are simple without any annotations and Date-properties initialization in both cases look like this:

    Date openDate = null;

    if (currency.getOpenDate() != null) {
        openDate = new Date(currency.getOpenDate().getTime());
    }

Server side instance

enter image description here

JSON response

enter image description here

1
Obviously currencyOpenedDate doesn't have a time component that is why it is not considered as a timestamp but instead just a Date. - shazin
Thanks, but how to make the default behavior for Jackson-serializer for the Dates with and without time-components to convert them to timestamps? - Michael

1 Answers

2
votes

Configure your Object Mapper as follows;

@Autowired
private ObjectMapper objectMapper;

objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, true);