有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当映射到POJO中的双类型字段时,java Jackson ObjectMapper将整数值转换为null

请容忍我。我已经通过了很多链接,但我无法找到解决我的情况。我需要帮助

注意:我不能更改JSON请求(在测试中表示为map)

以下是我的建议:

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());行中,频率为空。因此测试失败

我希望它能自动转换成双色。 把@JsonIgnore放在另一个二传手上也没用

任何从地图中获取有效对象的方法都可以


共 (0) 个答案