JsonProperty在Spring Boot中被忽略了

huangapple go评论64阅读模式
英文:

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;
    }

}

huangapple
  • 本文由 发表于 2020年8月20日 22:48:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63507661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定