无法使用JSONSCHEMA2POJO从JSON对象到Java Pojo生成DateTime字段



我有以下JSON对象:

{
  "period": {
    "startDate": "2016-09-01T14:39:13.884Z",
    "endDate": "2021-09-01T14:39:13.884Z"
}

,在我的Maven插件配置中,我添加了

<plugin>
    <groupId>org.jsonschema2pojo</groupId>
    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
    <version>0.4.23</version>
    <configuration>
        <sourceType>json</sourceType>
    </configuration>
    <executions>
        <execution>
            <id>id</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                ...
                <dateTimeType>java.time.ZonedDateTime</dateTimeType>
            </configuration>
        </execution>
    </executions>
</plugin>

我正在使用Java8。但是在我生成的Java类中:

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "startDate",
    "endDate"
})
public class Period {
    @JsonProperty("startDate")
    private String startDate;
    @JsonProperty("endDate")
    private String endDate;
    ...
}

字段的类型被识别为字符串。是否可以使用正确的DateTime类型直接从JSON对象生成?还是我必须使用JSON模式来执行此操作?

这对我有用,请参阅下面的Maven插件设置。我可以看到的唯一有意义的区别是,您将JSON用作源,我正在使用JSON模式文件。您可以尝试创建一个模式文件:

    {
        "$schema": "http://json-schema.org/draft-03/schema",
        "title": "My Title",
        "type": "object",
        "description": "The departure time info",
        "properties": {
            "scheduled": {
                "type": "string",
                "format": "date-time",
                "required": true,
                "description": "Scheduled time of departure."
            },
         .
         .
         .
    }

这是我的Maven插件配置:

        <plugin>
            <groupId>org.jsonschema2pojo</groupId>
            <artifactId>jsonschema2pojo-maven-plugin</artifactId>
            <version>0.4.32</version>
            <configuration>
                <sourceDirectory>${basedir}/src/main/resources/schema/model</sourceDirectory>
                <targetPackage>myPackage</targetPackage>
                <useJodaDates>false</useJodaDates>
                <useJodaLocalTimes>false</useJodaLocalTimes>
                <useJodaLocalDates>false</useJodaLocalDates>
                <includeJsr303Annotations>false</includeJsr303Annotations>
                <includeAdditionalProperties>false</includeAdditionalProperties>
                <dateTimeType>java.time.ZonedDateTime</dateTimeType>
                <dateType>java.time.LocalDate</dateType>
                <generateBuilders>true</generateBuilders>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

最新更新