WRITE_DATES_AS_TIMESTAMPS没有在Spring boot 1.3.5上进行过woking

时间:2016-07-01 18:48:37

标签: json spring-mvc spring-boot jackson

我无法让杰克逊正确地序列化日期。

它始终序列化为:

{"now":[2016,7,1,15,41,44,753]}

而不是:

{"now":"2016-07-01T15:41:44.753"}

这是我的来源:

的src /主/ JAVA / BR / COM / JJW /弹簧/杰克逊/错误/ App.java

package br.com.jjw.spring.jackson.bug;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

的src /主/ JAVA / BR / COM / JJW /弹簧/杰克逊/错误/ MvcConfig.java

package br.com.jjw.spring.jackson.bug;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/public/");
    }

}

的src /主/ JAVA / BR / COM / JJW /弹簧/杰克逊/错误/ MyRestController.java

package br.com.jjw.spring.jackson.bug;

import java.util.Collections;
import java.util.Map;

import org.joda.time.LocalDateTime;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {

    @RequestMapping("/get")
    public Map<String, Object> get() {
        return Collections.singletonMap("now", new LocalDateTime());
    }

}

的src /主/资源/ application.properties

server.port=3000
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
spring.jackson.date-format=yyyy-MM-dd'T'HH:mm:ss.SSS

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>br.com.jjw</groupId>
    <artifactId>spring-jackson-bug</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-joda</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,我刚刚将MvcConfig更改为WebMvcConfigurerAdapter

package br.com.jjw.spring.jackson.bug;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/public/");
    }

}

要添加,此帖子有助于了解Difference between WebMvcConfigurationSupport and WebMvcConfigurerAdapter

的原因