我有一个 Spring 控制器类,它返回具有 @ResponseBody 的 JSON。但奇怪的是,它为双字段返回整数。我使用 JacksonMapper 作为 JSON 库。这是课程。
Phone
public class Phone implements Serializable {
@JsonProperty(value="phoneid")
private long phoneId;
private Plan plan;
private String sim;
private String imei;
@JsonProperty(value="phonetype")
private String phoneType;
@JsonProperty(value="phonenumber")
private String phoneNumber;
private String label;
@JsonProperty(value="connecteddate")
private String connectedDate;
//getters and setters
}
Plan
public class Plan implements Serializable {
@JsonProperty(value="planid")
private long planId;
@JsonProperty(value="planname")
private String planName;
private double billingIncrement;
private double owiStdUnitCost;
private double owiFlagFall;
private double stdCap;
private double dataCap;
private double smsCap;
private double owiDataUnitCost;
private double owiSms;
//getters and setters
}
PhoneResult
public class PhoneResult implements Serializable {
private boolean ok;
private String message;
private Phone result;
//getters and setters
}
当我返回 PhoneResult 对象时,它返回如下:
{
"ok": true,
"message": "",
"result":
{
"phoneid": 600003,
"phonenumber": "12343423",
"phonetype": "Samsung Galaxy S2",
"imei": "343242",
"sim": "1234324",
"label": "Mr Bean",
"connecteddate": "2012-09-19 00:00:00.0",
"plan":
{
"planid": 34,
"planname": "$59 Jeenee Plan",
"billingIncrement": 30,
"owiStdUnitCost": 81.8181818181818,
"owiFlagFall": 0,
"stdCap": 636.3636,
"dataCap": 227.2665,
"smsCap": 1363.638,
"owiDataUnitCost": 0.022194,
"owiSms": 22.7272727272727
}
}
}
billingIncrement 和 owiFlagFall 具有整数类型,即使这些字段在类和数据库中是双精度类型。我不想关心这个,但是当我使用 Spring resttemplate 再次将返回的 JSON 转换为对象时会导致错误。如果这些字段具有像 30.0 和 0.0 这样的双重类型,则不会导致任何错误。这看起来很奇怪,但这是真的。你能给我什么建议吗?您的回答将不胜感激。