sebastianlee sebastianlee - 3 months ago 36
JSON Question

Jackson Date Format from HashMap

I have a problem with jackson outputting my date in the below format.

{"DOB":1173196800000}


However, I want it to be something more readable. I have seen solutions using the jsonDateSerializer and Deserializer class and has some success in using it for POJO related implementation.

Jackson date deserialization

The problem I am facing right now would be the mapping of sql objects to a hashmap instead of a POJO. I am unclear where I could specify the object mapping part for hashmap

public @ResponseBody ArrayList<HashMap<String,Object>> sqlObjectRetrieval(){
ArrayList<HashMap<String,Object>> retrievedObjects =
objectFromSql.retrieved(1,2,3);
}


I am using Spring3 and jackson for this implementation

Thanks in advance

Answer

Try to add

    <mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                    <property name="dateFormat">
                        <bean class="java.text.SimpleDateFormat">
                            <constructor-arg value="yyyy-MM-dd HH:mm:ss"/>
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

in your spring-mvc.xml