Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 反序列化JSON以创建POJO和相关引用_Java_Json_Oracle Apex_Pojo - Fatal编程技术网

Java 反序列化JSON以创建POJO和相关引用

Java 反序列化JSON以创建POJO和相关引用,java,json,oracle-apex,pojo,Java,Json,Oracle Apex,Pojo,我正在尝试使用Oracle RESTful API创建一个与另一个POJO相关的POJO 上下文 让我先解释一下我要处理的上下文 考虑以下POJO: @JsonIgnoreProperties(ignoreUnknown = true) public class Brand { private Pencil[] pencils; // standard getters and setters } @JsonIgnoreProperties(ignoreUnknown = tru

我正在尝试使用Oracle RESTful API创建一个与另一个POJO相关的POJO

上下文 让我先解释一下我要处理的上下文

考虑以下POJO:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Brand {
    private Pencil[] pencils;
    // standard getters and setters
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Pencil {
    private int length;
    private Brand brand;
    private Color color;
    // standard getters and setters
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Color {
    private int red;
    private int green;
    private int blue;
    // standard getters and setters
}
APEX中的数据库结构如下所示:

CREATE TABLE Brand (
    id int NOT NULL,
    PRIMARY KEY (id)
);
CREATE TABLE Pencil (
    id int NOT NULL,
    length int NOT NULL,
    brand_id int NOT NULL,
    color_id int NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (brand_id) REFERENCES Brand(id),
    FOREIGN KEY (color_id) REFERENCES Color(id)
);
CREATE TABLE Color (
    id int NOT NULL,
    red int NOT NULL,
    green int NOT NULL,
    blue int NOT NULL,
    PRIMARY KEY (id)
);
ORDS URI模板是
。/brand/{brand_ID}/pencils
,并使用以下get请求处理程序源和源类型“Query”:

请求可能返回类似以下JSON的内容:

{
    "items": [
        {
            "length": 100,
            "red": 150,
            "green": 200,
            "blue": 250,
        },
        {
            "length": 50,
            "red": 100,
            "green": 150,
            "blue": 200,
        }
    ],
    "first:" {
        "$ref": "..."
    }
}
问题 在创建POJO的客户端读取此JSON时,我遇到了一个问题。我能够将铅笔实例的长度属性设置为正确的值。但是,Pencil实例的color属性将保持
null
。我猜这是因为Pencil不知道JSON属性red、green和blue,因此被忽略了。如果我是正确的,那么我可能有两个解决方案,但对于其中任何一个,我都不知道如何实现它们

一些可能的解决办法
  • 通过注释以颜色类的属性为目标,并停止忽略它们,声明它们可用于何处
  • 以某种方式使用键
    color
    和color类的属性向响应中的每个数组项添加一个JSON对象

  • 我想有几件事你可以试试

  • 您可以修改json响应对象以与数据建模POJO相匹配

  • 如果无法修改json响应以匹配客户端数据建模,则可以创建与json响应匹配的JsonDto,然后根据需要使用JsonDto手动构建POJO

  • 希望这有帮助。:)

    {
        "items": [
            {
                "length": 100,
                "red": 150,
                "green": 200,
                "blue": 250,
            },
            {
                "length": 50,
                "red": 100,
                "green": 150,
                "blue": 200,
            }
        ],
        "first:" {
            "$ref": "..."
        }
    }