Jackson 1.9 configuration for spring 3.1 (for global date set) Allowed

Welcome everyone. I am successfully using Spring 3.1 mvc to create leisure web services using json as an http message. Unitl now I set the designation in each beans field to use a custom serializer / deserializer and asked Jackson to format my date in a specific format. Now, I would like to remove this notation syntax and set the global date format. This is how i did it

this is my servlet configuration

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <context:component-scan base-package="com.test.endpoints" /> <mvc:annotation-driven> <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="objectMapper"> <bean class="com.test.CustomObjectMapper" /> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans> 

this is a CustomObjectMapper class

 package com.test; import java.text.SimpleDateFormat; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig.Feature; public class CustomObjectMapper extends ObjectMapper { public CustomObjectMapper() { super(); configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false); setDateFormat(new SimpleDateFormat( "EEE MMM dd yyyy HH:mm:ss 'GMT'ZZZ (z)")); } } 
+4
source share
1 answer

Add below xml to mvc configuration to redefine date serialization worldwide.

 <mvc:annotation-driven > <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" > <property name="objectMapper"> <bean class="package.CustomObjectMapper"/> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> 

and change the CustomObjectMapper class as shown below.

 import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig.Feature; public class CustomObjectMapper extends ObjectMapper { public CustomObjectMapper(){ super.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false); } } 
0
source

Source: https://habr.com/ru/post/1433719/


All Articles