我无法让杰克逊正确地序列化日期。
它始终序列化为:
{"now":[2016,7,1,15,41,44,753]}
代替:
{"now":"2016-07-01T15:41:44.753"}
这是我的来源:
src / main / java / br / com / jjw / spring / jackson / bug / 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 / main / java / br / com / jjw / spring / jackson / bug / 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 / main / java / br / com / jjw / spring / jackson / bug / 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 / main / resources / 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>
我找到了一个解决方案,我只是将我的更改MvcConfig为extends WebMvcConfigurerAdapter。
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/"); } }