我正在为返回布尔值“ 0”和“ 1” 的服务器编写JSON客户端。当我尝试运行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 Client Line
注1:这是使用Spring 3.2
注2: toJSONString()-是使用Jackson序列化我的Parameters对象的辅助方法
注3:读取IN结果对象时发生异常
DocInfoResponse result = restTemplate.getForObject(docInfoURI.toString()
+ "/?input={input}",
DocInfoResponse.class,
toJSONString(params));
正如Paulo Pedroso的答案所提及和引用的那样,您将需要遵循自己的习惯JsonSerializer
和JsonDeserializer
。创建完成后,您需要在属性中添加@JsonSerialize
和@JsonDeserialize
注释;指定用于每个类的类。
我在下面提供了一个小示例(希望很简单)。序列化程序和反序列化程序的实现都不是超级健壮的,但这应该可以帮助您入门。
public static class SimplePojo {
@JsonProperty
@JsonSerialize(using=NumericBooleanSerializer.class)
@JsonDeserialize(using=NumericBooleanDeserializer.class)
Boolean bool;
}
public static class NumericBooleanSerializer extends JsonSerializer<Boolean> {
@Override
public void serialize(Boolean bool, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException {
generator.writeString(bool ? "1" : "0");
}
}
public static class NumericBooleanDeserializer extends JsonDeserializer<Boolean> {
@Override
public Boolean deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException {
return !"0".equals(parser.getText());
}
}
@Test
public void readAndWrite() throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
// read it
SimplePojo sp = mapper.readValue("{\"bool\":\"0\"}", SimplePojo.class);
assertThat(sp.bool, is(false));
// write it
StringWriter writer = new StringWriter();
mapper.writeValue(writer, sp);
assertThat(writer.toString(), is("{\"bool\":\"0\"}"));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句