Spring 3.1 JSON date format

后端 未结 3 1671
挽巷
挽巷 2020-11-30 00:45

I am using annotated Spring 3.1 MVC code (spring-mvc) and when i send date object through the @RequestBody the date is showing up as numeric. This is my controller

相关标签:
3条回答
  • 2020-11-30 01:04

    Here is a more standard way to configure this, using ISO8601 dates, which is what I would recommend for your API.

    <!-- you can't call it objectMapper for some reason -->
    <bean name="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
        <property name="featuresToDisable">
            <array>
                <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS"/>
            </array>
        </property>
    </bean>
    
    <!-- setup spring MVC -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper" ref="jacksonObjectMapper"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    

    Here is additional documentation:

    • Jackson FAQ
    • Spring's Jackson2ObjectMapperFactoryBean
    0 讨论(0)
  • 2020-11-30 01:10

    Alternatively if you are using jackson and want an ISO-8601 date on all dates (not just those you annotate), you can disable the default of writing dates as timestamps.

    <bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>
    <bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig" factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />
    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="jacksonSerializationConfig" />
        <property name="targetMethod" value="disable" />
        <property name="arguments">
            <list>
                <value type="org.codehaus.jackson.map.SerializationConfig.Feature">WRITE_DATES_AS_TIMESTAMPS</value>
            </list>
        </property>
    </bean>
    

    Then if you want to convert your dates into some other format than the default, you can do this:

    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="jacksonSerializationConfig" />
        <property name="targetMethod" value="setDateFormat" />
        <property name="arguments">
            <list>
              <bean class="java.text.SimpleDateFormat">
                <constructor-arg value="yyyy-MM-dd'T'HH:mm:ssZ"/>
              </bean>
            </list>
        </property>
    </bean>
    
    0 讨论(0)
  • 2020-11-30 01:21

    In order to override the default date formatting strategy of Jakson following are the step to follow:

    1. Extend JsonSerializer to create a new class for handling date formatting
    2. Override serialize(Date date, JsonGenerator gen, SerializerProvider provider) function to format date in your desired format and write it back to generator instance (gen)
    3. Annotate your date getter object to use your extended json serializer using @JsonSerialize(using = CustomDateSerializer.class)

    Code:

    //CustomDateSerializer class
    public class CustomDateSerializer extends JsonSerializer<Date> {    
        @Override
        public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2) throws 
            IOException, JsonProcessingException {      
    
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = formatter.format(value);
    
            gen.writeString(formattedDate);
    
        }
    }
    
    
    //date getter method
    @JsonSerialize(using = CustomDateSerializer.class)
    public Date getDate() {
        return date;
    }
    

    Source: http://blog.seyfi.net/2010/03/how-to-control-date-formatting-when.html

    0 讨论(0)
提交回复
热议问题