java - 无法使用 jsonschema2pojo 从 json 对象生成 DateTime 字段到 java pojo?

标签 java json

我有以下 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;
    ...
}

字段的类型被识别为字符串。是否可以直接从 json 对象生成正确的 DateTime 类型?或者我必须使用 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>

关于java - 无法使用 jsonschema2pojo 从 json 对象生成 DateTime 字段到 java pojo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37856256/

相关文章:

json - 如何公开 REST API HAL 格式分页

html - 如何实现D3层级 View

java - java中的Facebook oauth身份验证仅返回用户名和id

java - Java中字节数组的内存使用

java - 由于输入结束 jackson 解析器,没有要映射的内容

javascript - 如何将jquery中的迭代json对象传递给javascript函数

java - 获取带有 Json 响应的请求?

java - 无法在 Android 中使用 BroadcastReceiver 上下文初始化接口(interface)

java - 检测构造函数中的 final 是否为空

java - 无法纠正我的 java 程序