Jackson ObjectMapper 在映射到 POJO 中的 Double 类型字段时将 Integer 值转换为 null
Jackson ObjectMapper converts Integer value to null when mapped to Double type field in POJO
请多多包涵。我浏览了很多链接,但找不到适合我案例的解决方案。我需要帮助。
注意:我无法更改 JSON 请求(在测试中表示为地图)
这是我的 POJO:
public class TestModelWithDoubleField {
private Double frequency;
public Double getFrequency(){
return frequency;
}
/**
* @param frequency the frequency to set
*/
public void setFrequency(Double frequency) {
this.frequency = frequency;
}
/**
* @param frequency the frequency to set
*/
@JsonIgnore
public void setFrequency(Integer frequency) {
if(frequency != null) {
setFrequency(new Double(frequency));
}
}
}
这是失败的测试:
@Test
public void testWithIntegerValueConvertToDoubleFieldInPOJO() throws IOException {
final Map<String, Integer> map = new HashMap<>();
map.put("frequency", 900);
TestModelWithDoubleField pojo = objectMapper.convertValue(map, TestModelWithDoubleField.class);
Assert.assertNotNull(pojo);
Assert.assertNotNull(pojo.getFrequency()); //-> This is giving output as null. Hence fails.
}
行Assert.assertNotNull(pojo.getFrequency());
中的频率为空。因此测试失败。
我希望它自动转换为它的 Double 类型。
将 @JsonIgnore
放在另一个 setter 上也没有用。
从该地图中获取有效对象的任何方法都可以。
只需删除 public void setFrequency(Integer frequency)
方法即可:
public class TestModelWithDoubleField {
private Double frequency;
public Double getFrequency(){
return frequency;
}
public void setFrequency(Double frequency){
this.frequency = frequency;
}
}
既然你不能删除public void setFrequency(Integer frequency)
方法,那么你可以用@JsonProperty
注释另一个setter定义它作为Jackson使用的setter :
public class TestModelWithDoubleField {
private Double frequency;
public Double getFrequency(){
return frequency;
}
@JsonProperty
public void setFrequency(Double frequency) {
this.frequency = frequency;
}
public void setFrequency(Integer frequency) {
if(frequency != null) {
setFrequency(new Double(frequency));
}
}
}
只需在所需的 setter 上添加 @JsonProperty("frequency")
。你甚至不需要 @JsonIgnore
在另一个上。
public static class TestModelWithDoubleField
{
private Double frequency;
public Double getFrequency()
{
return frequency;
}
/**
* @param frequency the frequency to set
*/
@JsonProperty("frequency")
public void setFrequency(Double frequency)
{
this.frequency = frequency;
}
/**
* @param frequency the frequency to set
*/
public void setFrequency(Integer frequency)
{
if(frequency != null)
{
setFrequency(new Double(frequency));
}
}
}
请多多包涵。我浏览了很多链接,但找不到适合我案例的解决方案。我需要帮助。
注意:我无法更改 JSON 请求(在测试中表示为地图)
这是我的 POJO:
public class TestModelWithDoubleField {
private Double frequency;
public Double getFrequency(){
return frequency;
}
/**
* @param frequency the frequency to set
*/
public void setFrequency(Double frequency) {
this.frequency = frequency;
}
/**
* @param frequency the frequency to set
*/
@JsonIgnore
public void setFrequency(Integer frequency) {
if(frequency != null) {
setFrequency(new Double(frequency));
}
}
}
这是失败的测试:
@Test
public void testWithIntegerValueConvertToDoubleFieldInPOJO() throws IOException {
final Map<String, Integer> map = new HashMap<>();
map.put("frequency", 900);
TestModelWithDoubleField pojo = objectMapper.convertValue(map, TestModelWithDoubleField.class);
Assert.assertNotNull(pojo);
Assert.assertNotNull(pojo.getFrequency()); //-> This is giving output as null. Hence fails.
}
行Assert.assertNotNull(pojo.getFrequency());
中的频率为空。因此测试失败。
我希望它自动转换为它的 Double 类型。
将 @JsonIgnore
放在另一个 setter 上也没有用。
从该地图中获取有效对象的任何方法都可以。
只需删除 public void setFrequency(Integer frequency)
方法即可:
public class TestModelWithDoubleField {
private Double frequency;
public Double getFrequency(){
return frequency;
}
public void setFrequency(Double frequency){
this.frequency = frequency;
}
}
既然你不能删除public void setFrequency(Integer frequency)
方法,那么你可以用@JsonProperty
注释另一个setter定义它作为Jackson使用的setter :
public class TestModelWithDoubleField {
private Double frequency;
public Double getFrequency(){
return frequency;
}
@JsonProperty
public void setFrequency(Double frequency) {
this.frequency = frequency;
}
public void setFrequency(Integer frequency) {
if(frequency != null) {
setFrequency(new Double(frequency));
}
}
}
只需在所需的 setter 上添加 @JsonProperty("frequency")
。你甚至不需要 @JsonIgnore
在另一个上。
public static class TestModelWithDoubleField
{
private Double frequency;
public Double getFrequency()
{
return frequency;
}
/**
* @param frequency the frequency to set
*/
@JsonProperty("frequency")
public void setFrequency(Double frequency)
{
this.frequency = frequency;
}
/**
* @param frequency the frequency to set
*/
public void setFrequency(Integer frequency)
{
if(frequency != null)
{
setFrequency(new Double(frequency));
}
}
}