无法使用camel marshling格式化LocalDate对象

时间:2018-03-05 07:13:18

标签: apache-camel

我正在使用驼峰路由将用户对象保存到文件,同时保存我正在使用camel marshaling选项将用户对象转换为json。 但是编组它时会以下列格式保存日期对象

"birthDate":{"year":2018,"month":"FEBRUARY","era":"CE","dayOfMonth":20,"dayOfWeek":"TUESDAY","dayOfYear":51,"leapYear":false,"monthValue":2,"chronology":{"id":"ISO","calendarType":"iso8601"}}

所需的格式是

  "birthDate":"20-feb-2018"

这是我的POJO

public class User implements Serializable{

    private String id;

    private String fName;

    private String lName;

    private String email;

    private Integer  pinCode;

    @JsonFormat(pattern="dd-MMM-yyyy")
    private LocalDate birthDate;

    private Boolean isActive;
}

路由网址

from("direct:saveUserJson").marshal().json(JsonLibrary.Jackson,User.class).to("file:"+filePath+"?fileName=${exchangeProperty.fileName}");

pom.xml我使用的是camel-jackson-starter和camel-spring-boot-starter版本2.20.1

<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.igtb</groupId>
    <artifactId>assignment1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>assignment1</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.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>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

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

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>2.20.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jackson-starter</artifactId>
            <version>2.20.1</version>
        </dependency>
    </dependencies>
</project>

1 个答案:

答案 0 :(得分:0)

不确定,但是当您将形状String添加到注释时,它可能会起作用:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MMM-yyyy")