如何阻止杰克逊在 SpringMVC 中将日期写为时间戳

JEE*_*ARI 3 java rest spring spring-mvc

我一直在阅读有关 Spring boot 的教程,在那里我学会了如何阻止 jackson 将日期转换为时间戳。例如:

{"birthDate":1505736233603} //before
{"birthDate":"2017-09-18T12:04:27.345+0000"}//after
Run Code Online (Sandbox Code Playgroud)

通过写作

 spring.jackson.serialization.write-dates-as-timestamps=false
Run Code Online (Sandbox Code Playgroud)

application.properties 中。

我该怎么做用SpringMVC为同一着,当然也没有application.properties用SpringMVC

Fer*_*nto 7

MessageConverter另一种方法是在框架填充/配置它们之后进行配置:

@EnableWebMvc
@Configuration
public class AppConfiguration implements WebMvcConfigurer {
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter<?> converter : converters) {
            if (converter instanceof AbstractJackson2HttpMessageConverter) {
                AbstractJackson2HttpMessageConverter jacksonconverter = (AbstractJackson2HttpMessageConverter) converter;
                jacksonconverter.getObjectMapper()
                        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,这段代码也应该适用于SMILEbecause MappingJackson2SmileHttpMessageConverterextends AbstractJackson2HttpMessageConverter


j_p*_*rez 6

You'll have to configure the Spring Bean in charge of creating the JSON your service is returning.

First off, you need to define the Jackson Object Mapper Bean that your converter will use to create the JSON:

<bean class="com.fasterxml.jackson.databind.ObjectMapper" id="objectMapper">
    <property name="dateFormat">
        <bean class="java.text.SimpleDateFormat">
            <constructor-arg value="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"/>
        </bean>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

(NOTE that you can define the dateFormat that you need).

Then, you need to inject this objectMapper bean into the JSON Message converter:

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

    </mvc:message-converters>
</mvc:annotation-driven>    
Run Code Online (Sandbox Code Playgroud)

As you can see, I'm using the "mvc" namespace ("http://www.springframework.org/schema/mvc") to define the MVC beans.

If you're using Annotations rather than XML configuration, you can do exactly the same by defining the next Configuration class (or adapt it to you code :) )

@EnableWebMvc
@Configuration
@ComponentScan({ "com.yourorg.app" })
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(
        List<HttpMessageConverter<?>> converters) {
        messageConverters.add(new createJsonHttpMessageConverter()); 
        super.configureMessageConverters(converters);
    }

    private HttpMessageConverter<Object> createJsonHttpMessageConverter() {

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));

        MappingJackson2HttpMessageConverter jsonConverter = 
           new MappingJackson2HttpMessageConverter();
        jsonConverter.setObjectMapper(objectMapper);

        return jsonConverter;
    }
}
Run Code Online (Sandbox Code Playgroud)

Hope this helps :)