Spring Boot 2.04 Jackson无法将LocalDateTime序列化为String

时间:2018-09-11 13:31:53

标签: java spring spring-boot jackson swagger

我有序列化为JSON数组而不是String的Spring Boot应用程序和LocalDateTime属性。

正如我在网络上搜索时所发现的,我只需要在 application.properties

中进行设置
spring.jackson.serialization.write-dates-as-timestamps=false

我还尝试将 jackson-datatype-jsr310 设置为依赖项,但也没有运气

我也尝试添加注释:

@DateTimeFormat(iso = ISO.DATE_TIME)

它也无济于事。

我看到很多人遇到类似的情况,但是他们的解决方案似乎与Spring Boot 1.x有关,而我使用的是2.04

我也使用Lombok,但不确定是否会影响序列化格式。

如何跟踪日期序列化格式并将其固定为ISO日期字符串?

这里是响应示例(开始是LocalDateTime,我想将其作为ISO字符串):

{
  "id": 3,
  "enabled": true,
  "outletId": 5,
  "reason": "hello",
  "start": [
    2019,
    9,
    10,
    10,
    42,
    11
  ],
  "status": "AVAILABLE"
}

这里是REST控制器方法响应对象:

@Data
@Entity
@Table(indexes = { @Index(columnList = ("outletId"),name="outlet_id_index"), 
        @Index(columnList = ("start"),name="start_index"),
        @Index(columnList = ("outletId, start"),name="outlet_id_start_index")})
public class OutletChron extends BaseEntity {
    private Long outletId;
    private String reason;

    @DateTimeFormat(iso = ISO.DATE_TIME)
    private LocalDateTime start;
    @Enumerated(EnumType.STRING)
    @Column(length = 30)
    private OutletStatus status;
}

这是我的POM:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.banquets</groupId>
    <artifactId>Banquet</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Banquet</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> 
            </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 
            </dependency> -->

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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


</project>

更新

我建立了一个新项目进行测试,结果发现 LocalDateTime 映射的默认格式为 String 。配置swagger后,我能够跟踪格式更改。因此,如果没有这种灵活的配置,我将具有String格式:

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {

    @Autowired
    ServletContext servletContext;

    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.demo"))
                .build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

更新,此Swagger配置似乎可行(日期格式为String,我可以通过http://localhost:8000/api/swagger-ui.html访问Swagger UI

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket apiDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

3 个答案:

答案 0 :(得分:3)

对我来说,以下作品:

@JsonFormat(pattern = "dd.MM.yyyy HH:mm")
private LocalDateTime startTime;

这将以字符串格式打印日期,例如就像 11.09.2018 15:44

答案 1 :(得分:3)

Spring boot 2.x不需要导入JSR310规范,因为它现在是spring框架的一部分,因此不需要导入它们,并且字符串格式将自动起作用。

如果您需要覆盖Spring Boot的某些默认配置,则需要实现WebMvcConfigurer而不是扩展WebMvcConfigurationSupport

在您的情况下,如果要将静态文件放在默认资源文件夹中以外的其他位置,则可能需要覆盖addResourceHandlers并注册路径。

如果默认路径中的资源不是必需的,则只需删除扩展名WebMvcConfigurationSupport就可以使用默认字符串格式。

更新后的答案:

如果使用WebMvcConfigurationSupport,则表明它不应该自动配置Spring MVC,这意味着默认设置将不起作用,您必须在此处通过覆盖其支持方法来定义所有内容。因此,请代替WebMvcConfigurationSupport来实现WebMvcConfigurer

这是更新的配置。

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.demo"))
                .build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

答案 2 :(得分:0)

手动创建对象映射器

@Bean
@Primary
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();

    JavaTimeModule timeModule = new JavaTimeModule();
    mapper.registerModule(timeModule);

    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);

    return mapper;
}