Spring - 忽略请求中的 JSON 属性但不响应

Spring - Ignore JSON properties in request but not in response

这是从 GET 请求 http://domain/entry/{id}:

中检索到的有效条目 object
{
    "id": 1,
    "description": "ML Books",
    "dueDate": "2017-06-10",
    "paymentDate": "2017-06-10",
    "note": "Distribution de lucros",
    "value": 6500,
    "type": "INCOME",
    "category": {
        "id": 2,
        "name": "Food"
    },
    "person": {
        "id": 3,
        "name": "User",
        "active": true,
        "address": {
            // properties suppressed for better reading
        }
    }
}

在 POST 请求中我想保存 objects CategoryPerson 只是发送各自的 ID,像这样:

{
    "description": "NEW ENTRY",
    "dueDate": "2019-06-22",
    "paymentDate": "2019-06-22",
    "note": "Coloured pens",
    "value": 10,
    "type": "INCOME",
    "categoryId": 5,
    "personId": 5
}

为了保存 object 而不 Spring 说明此人和类别 object 是 null,我在其中添加了 @JsonIgnore模型,并遵循 this 线程。

部分有效:

现在,当使用相同的 GET 请求检索条目时 http://domain/entry/{id}:

{
    "id": 23,
    "description": "Pens",
    "dueDate": "2019-06-22",
    "paymentDate": "2019-06-22",
    "note": "Coloured pens",
    "value": 10,
    "type": "INCOME",
    "categoryId": null, // It supposed to bring the entire object
    "personId": null // It supposed to bring the entire object
}

PS:categoryId和personId用@Transient标记,所以为null。

因此,正如标题所述,我只想在 POST 请求(保存它们)中忽略属性 Category 和 Person,而不是在 GET 请求(检索它们)中。

欢迎任何帮助。 提前致谢

Jackson 为 JsonProperty 添加了 READ_ONLYWRITE_ONLY 注释参数。所以你也可以这样做:

@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@Id
@GeneratedValue
private  Long  id;