Spring Boot与Jackson一起串行化java.time.LocalDateTime以返回ISO-8601 JSON时间戳?

sa1*_*125 15 serialization jackson java-8 spring-boot java-time

我正在努力在spring-boot REST API应用程序中转换一些模型,以使用java 8 java.time.LocalDateTime而不是joda DateTime.我希望API调用返回的时间戳符合ISO_8601格式.动机是与Java 8的时间向前兼容(更多这里).

证明困难的部分是序列化包含LocalDateTimeJSON 的对象.

例如,我有以下实体:

// ... misc imports

import java.time.LocalDateTime;
import java.time.ZoneOffset;

@Data 
@Entity
@Table(name = "users")
public class User {

    @Id @Column
    private String id;

    @Column
    private String name;

    @Column
    private String email;

    @Column
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss", timezone = "UTC")
    private java.time.LocalDateTime createdAt;

    public User(String name, String email) {
        this.id = Utils.generateUUID();
        this.createdAt = LocalDateTime.now(ZoneOffset.UTC);
    }
}
Run Code Online (Sandbox Code Playgroud)

我还设置了application.properties关闭日期作为时间戳杰克逊功能:

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
Run Code Online (Sandbox Code Playgroud)

我的maven deps:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.3.6.RELEASE</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.8.1</version>
</dependency>

<dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version>5.0.1.Final</version>
 </dependency>
Run Code Online (Sandbox Code Playgroud)

最后,我尝试通过控制器检索JSON表示:

@RequestMapping("/users")
@RestController
public class UserController {

    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(
        value = "/{id}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public User getUser(@PathVariable("id") String id) {
        return userService.findById(id);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我实际调用此端点时,我得到以下异常:

java.lang.NoSuchMethodError: 
com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase.findFormatOverrides(Lcom/fasterxml/jackson/databind/SerializerProvider;Lcom/fasterxml/jackson/databind/BeanProperty;Ljava/lang/Class;)Lcom/fasterxml/jackson/annotation/JsonFormat$Value;
Run Code Online (Sandbox Code Playgroud)

或者我也在ObjectMapper配置类中配置了应用程序:

@Configuration
public class ServiceConfiguration {

    @Bean
    public ObjectMapper getJacksonObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.configure(
            com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
            false
        );
        return objectMapper;
    }
}
Run Code Online (Sandbox Code Playgroud)

任何线索将不胜感激.


更新:

原来这是Spring Boot的Jackson版本和我的版本之间的版本不匹配pom.xml.正如Miloš和Andy所提出的,一旦我设置了正确的版本并运行应用程序spring.jackson.serialization.write_dates_as_timestamps=true,问题就解决了,无需ObjectMapper在我的LocalDateTime模型字段上配置或添加注释.

    ...
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.3.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.3.6.RELEASE</version>
        </dependency>

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

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

    </dependencies>
Run Code Online (Sandbox Code Playgroud)

And*_*son 25

NoSuchMethodError是因为你正在混合杰克逊的版本.Spring Boot 1.3.6使用Jackson 2.6.7而你使用的是2.8.1 jackson-datatype-jsr310.

Spring Boot为Jackson提供依赖管理,包括jackson-datatype-jsr310,所以你应该从你的pom中删除版本.如果您想使用不同版本的Jackson,您应该覆盖该jackson.version属性:

<properties>
    <jackson.version>2.8.1</jackson.version>
</properties>
Run Code Online (Sandbox Code Playgroud)

这将确保您的所有Jackson依赖项具有相同的版本,从而避免版本不匹配的问题.

如果您愿意,也可以删除正在配置的Java代码ObjectMapper.Java Time模块将在类路径中自动注册,并在可以配置时间戳时编写日期application.properties:

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


Mil*_*vić 5

你的ObjectMapperbean必须被标记为@Primary以便被Spring接收.或者,您可以只创建一个JavaTimeModulebean,它将被Spring选中并添加到默认对象映射器中.

您可能已经看过了,但请查看官方文档.

  • 我刚注意到你使用2.8.6的Spring Boot和2.8 Jackson,但我不认为它们是兼容的.你能尝试使用Boot 1.4吗? (2认同)
  • 或者只使用Boot 1.3.6附带的jsr310 Jackson相同版本 - 我认为它应该是2.6.7 (2认同)