Spring Boot 2未序列化LocalDateTime [英] Spring Boot 2 not serializing LocalDateTime

查看:283
本文介绍了Spring Boot 2未序列化LocalDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试使用spring-boot 2实现微服务。

I recently tried to implement a micro service using spring-boot 2.

现在,每当我尝试返回包含的对象时,从我的REST服务获取java.time.LocalDateTime ,LocalDateTime被序列化为整数数组。像这样:

Now, whenever I attempt to return an object which contains a java.time.LocalDateTime from my REST service, the LocalDateTime get serialized as an array of integers. Like so:

{
  "id": "5bf1425f9f8de267f04b22ad",
  "description": "aaaaaarrrgggghhhhh",
  "timestamp": [
    2018,
    11,
    18,
    11,
    43,
    43,
    889000000
  ],
  "time": 2.25,
  ...
}

我尝试通过<$ c $中的设置配置 ObjectMapper c> application.yml

I have tried configuring the ObjectMapper through settings in application.yml

spring:
  jackson:
    serialization:
      write-dates-as-timestamps: false

但不起作用。我还尝试通过Spring Configuration类配置新的ObjectMapper,如下所示:

but doesn't work. I have also tried configuring a new ObjectMapper through a Spring Configuration class, like so:

@Configuration
public class JacksonConfig {
  @Bean
  @Primary
  public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
    final ObjectMapper objectMapper = builder.build();
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    return objectMapper;
  }
}

我的配置已加载(调试器在断点处停止)

My configuration gets loaded (debugger stops at a breakpoint) - it's just that it does nothing.

我尝试将 jackson 依赖项(也针对jsr310模块)手动添加到我的pom.xml-也没有运气。

I tried adding jackson dependencies manually (also for the jsr310 module) to my pom.xml - also without any luck.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
</dependency>

出于某种原因,看起来Spring Boot忽略了我使用ObjectMapper进行任何操作的尝试,并且一直返回相同的结果。

For some reason it looks like Spring Boot is ignoring my attempts to anything with the ObjectMapper, and it keeps returning the same result.

中将 com.fasterxml 的日志级别设置为DEBUG application.yml 也不会产生任何输出:

Setting log level to DEBUG for com.fasterxml in the application.yml also yields no output:

logging:
  level:
    com.fasterxml: DEBUG

我在Jackson 2.9.7上使用Spring Boot 2.1.0-RELEASE。

I use Spring Boot 2.1.0-RELEASE with Jackson 2.9.7.

基本pom文件是从 https://start.spring生成的.io 我的项目针对Java 8 JVM进行编译并运行。

The basic pom file was generated from https://start.spring.io My project compiles for and runs on a Java 8 JVM.

推荐答案

此答案基于teppic的评论

This answer is based on teppic's comment to the original post.

问题是由我的@Configuration类之一上的@EnableWebMVC引起的。删除@EnableWebMVC即可立即解决问题。

The issue was caused by @EnableWebMVC on one of my @Configuration classes. Removed @EnableWebMVC immediately solved the problem.

这篇关于Spring Boot 2未序列化LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆