如何将 FasterXML\Jackson 中的布尔值序列化/反序列化为 Int?
我正在为服务器编写一个JSON客户端,该客户端返回布尔值为“0”和“1”。当我尝试运行我的JSON客户端时,我目前遇到以下异常:
HttpMessageNotReadableException: Could not read JSON: Can not construct instance of java.lang.Boolean from String value '0': only "true" or "false" recognized
那么我该如何设置FasterXML\Jackson来正确解析如下内容:
{
"SomeServerType" : {
"ID" : "12345",
"ThisIsABoolean" : "0",
"ThisIsABooleanToo" : "1"
}
}
示例 Pojo 的:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"someServerType"})
public class myPojo
{
@JsonProperty("someServerType")
SomeServerType someServerType;
@JsonProperty("someServerType")
public SomeServerType getSomeServerType() { return someServerType; }
@JsonProperty("someServertype")
public void setSomeServerType(SomeServerType type)
{ someServerType = type; }
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"someServerType"})
public class SomeServerType
{
@JsonProperty("ID")
Integer ID;
@JsonProperty("ThisIsABoolean")
Boolean bool;
@JsonProperty("ThisIsABooleanToo")
Boolean boolToo;
@JsonProperty("ID")
public Integer getID() { return ID; }
@JsonProperty("ID")
public void setID(Integer id)
{ ID = id; }
@JsonProperty("ThisIsABoolean")
public Boolean getThisIsABoolean() { return bool; }
@JsonProperty("ThisIsABoolean")
public void setThisIsABoolean(Boolean b) { bool = b; }
@JsonProperty("ThisIsABooleanToo")
public Boolean getThisIsABooleanToo() { return boolToo; }
@JsonProperty("ThisIsABooleanToo")
public void setThisIsABooleanToo(Boolean b) { boolToo = b; }
}
Rest 客户端行
注释 1:这是使用 Spring 3.2
注释 2:toJSONString() - 是一个帮助器方法,它使用 Jackson 序列化我的参数对象
注释 3:读取结果对象时发生异常
DocInfoResponse result = restTemplate.getForObject(docInfoURI.toString()
+ "/?input={input}",
DocInfoResponse.class,
toJSONString(params));