英文:
JsonProperty is getting ignored in Spring Boot
问题
我想将我的 JSON 响应中的 _sddInfo
更改为 dropOffShippingInfo
,但是下面的 @JsonProperty 被忽略了。我该如何解决这个问题?
@JsonInclude(JsonInclude.Include.NON_NULL)
public class EligibleShippingMethods implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty("dropOffShippingInfo")
private DropOffShippingInfo _sddInfo;
public DropOffShippingInfo get_sddInfo() {
return _sddInfo;
}
public void set_sddInfo(DropOffShippingInfo _sddInfo) {
this._sddInfo = _sddInfo;
}
}
响应
"eligibleShippingMethods": [
{
"dropOffShippingInfo": {
"startTime": "2020-08-21T15:00:00Z",
"endTime": "2020-08-21T17:00:00Z",
"customerScheduleType": "window"
}
}
]
英文:
I want to change my json response _sddInfo to dropOffShippingInfo but below @JsonProperty getting ignored. How can I solve this issue?
@JsonInclude(JsonInclude.Include.NON_NULL)
public class EligibleShippingMethods implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty("dropOffShippingInfo")
private DropOffShippingInfo _sddInfo;
public DropOffShippingInfo get_sddInfo() {
return _sddInfo;
}
public void set_sddInfo(DropOffShippingInfo _sddInfo) {
this._sddInfo = _sddInfo;
}
}
response
"eligibleShippingMethods": [
"_sddInfo": {
"startTime": "2020-08-21T15:00:00Z",
"endTime": "2020-08-21T17:00:00Z",
"customerScheduleType": "window"
}
]
答案1
得分: 1
过去,Jackson 可能会支持序列化的名称与反序列化的名称不同。但现在不再支持这样做。因此,您应该使用 @JsonProperty("dropOffShippingInfo")
注解在一个 getter 方法上,它会期望传入的 JSON 数据载荷中也有 dropOffShippingInfo
。
英文:
In the past Jackson may have supported one name for serialization and another for deserialization. Not anymore. Therefore, you should annotate a getter with @JsonProperty("dropOffShippingInfo")
, it expects the incoming json payload to also have dropOffShippingInfo
答案2
得分: 0
在getter方法中添加`@JsonProperty("dropOffShippingInfo")`,并确保您导入了正确的包`com.fasterxml.jackson.annotation.JsonProperty`
@JsonInclude(JsonInclude.Include.NON_NULL)
public class EligibleShippingMethods implements Serializable {
private static final long serialVersionUID = 1L;
private DropOffShippingInfo _sddInfo;
@JsonProperty("dropOffShippingInfo")
public DropOffShippingInfo get_sddInfo() {
return _sddInfo;
}
public void set_sddInfo(DropOffShippingInfo _sddInfo) {
this._sddInfo = _sddInfo;
}
}
英文:
Add @JsonProperty("dropOffShippingInfo")
to getter method and make sure you are importing right package com.fasterxml.jackson.annotation.JsonProperty
@JsonInclude(JsonInclude.Include.NON_NULL)
public class EligibleShippingMethods implements Serializable {
private static final long serialVersionUID = 1L;
private DropOffShippingInfo _sddInfo;
@JsonProperty("dropOffShippingInfo")
public DropOffShippingInfo get_sddInfo() {
return _sddInfo;
}
public void set_sddInfo(DropOffShippingInfo _sddInfo) {
this._sddInfo = _sddInfo;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论