jackson Spring集成变压器:将LocalDateTime作为ISO字符串序列化到Json

20jt8wwn  于 2022-12-23  发布在  Spring
关注(0)|答案(1)|浏览(143)

使用Spring Integration,我需要将一个对象从外部API包转换为JSON,最后通过AmqpOutboundEndpoint发送它。
问题与该类的LocalDateTime属性有关:它们被序列化为整数数组[yyyy,mm,dd,HH,MM,ss],但我希望它们被转换为ISO 8601日期时间字符串。
我发现这个问题很常见,到目前为止,我已经将jackson-datatype-jsr 310添加到依赖项中,并设置了属性spring.jackson.serialization.write_data_as_timestamps: true,但这没有帮助。
我需要一个可行方案的提示,拜托。
编辑:添加源代码(删除包和导入行)和pom.xml:

@Configuration
@EnableAutoConfiguration
@IntegrationComponentScan
public class Application {

    public static void main(String[] args) throws InterruptedException {
        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);

        List<Data> source = Arrays.asList(new Data());
        ctx.getBean(Demo.class).demo(source);

        ctx.close();
    }

    @MessagingGateway
    public interface Demo {

        @Gateway(requestChannel = "upcase.input")
        void demo(Collection<Data> source);

    }

    public static class Data {

        public LocalDateTime getLocalDateTime() {
            return LocalDateTime.now();
        }

    }

    @Bean
    public IntegrationFlow upcase() {
        return f -> f
                .split()
                .log()
                .transform(new ObjectToJsonTransformer())
                .log()
                .handle(m -> System.out.printf("%n%s%n%n", m.getPayload()));
    }

}
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <spring.integration>5.5.15</spring.integration>
        <java.version>11</java.version>
    </properties>
    <groupId>com.example</groupId>
    <artifactId>spring-int</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-int</name>
    <description>Demo project for Spring Boot</description>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

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

</project>
mcvgt66p

mcvgt66p1#

我觉得你的期望正好相反。
LocalDateTimeSerializer的逻辑如下:

if (useTimestamp(provider)) {
        g.writeStartArray();
        _serializeAsArrayContents(value, g, provider);
        g.writeEndArray();
    }

因此,对于spring.jackson.serialization.write-dates-as-timestamps=true(默认情况下),我们实际上将拥有一个LocalDateTime属性数组。
如果您谈论ISO 8601的格式,那么您需要做完全相反的事情:spring.jackson.serialization.write-dates-as-timestamps=false。此配置修改:

public IntegrationFlow upcase(ObjectMapper objectMapper) {
  ...
  .transform(new ObjectToJsonTransformer(new Jackson2JsonObjectMapper(objectMapper)))

以便能够采用正确自动配置的ObjectMapper
上述LocalDateTimeSerializer将通过以下方式进行LocalDateTime化:

protected DateTimeFormatter _defaultFormatter() {
    return DateTimeFormatter.ISO_LOCAL_DATE_TIME;
}

使用此配置,输出如下所示:

{"localDateTime":"2022-11-28T13:17:30.4691295"}

相关问题