【问题标题】:Spring boot: Cannot serialize Int as FloatSpring boot:无法将 Int 序列化为 Float
【发布时间】:2018-10-29 19:04:29
【问题描述】:

我正在使用现成的 Jackson-databind 序列化器将整数字段作为浮点数返回:

@XmlElement(name = "toPick")
@Column(name = "to_pick")
@JsonSerialize(using = FloatSerializer.class)
private Integer toPick;

但我在序列化过程中收到此消息:

无法写入内容:java.lang.Integer 无法转换为 java.lang.Float

【问题讨论】:

    标签: java spring-boot jackson jackson-databind


    【解决方案1】:

    也许使用 IntegerSerializer?

    @XmlElement(name = "toPick")
    @Column(name = "to_pick")
    @JsonSerialize(using = IntegerSerializer.class)
    private Integer toPick;
    

    【讨论】:

    • 默认取整数一。我需要一个不同的序列化程序
    • “我需要不同的序列化程序” 为什么?
    • 它是您要序列化的类型.. 如果您查看序列化程序:serialize(String topic, Integer data) .. 它需要一个整数才能工作.. 不是浮点数
    猜你喜欢
    • 2019-02-16
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 2016-03-22
    • 2020-09-09
    • 1970-01-01
    相关资源
    最近更新 更多