Fasterxml Jackson自动将非布尔值转换为布尔值

我有一个pojo类,其中一个标志isControl是Boolean类型。

当此属性获得非布尔值而不是true or false fastxml jackson会自动将输入值转换为true 。 经过几个小时的调试后,我发现这是在setter方法setIsControl发生的。

如果此属性的输入值是非布尔值,我想传递自定义消息。 我编写了自己的注释来validation此属性的输入值,如果它不是布尔值,则返回自定义消息,但jackson在检查我的自定义validation器之前绑定该值。

使用jackson版>>> 2.6.3 。 任何帮助将不胜感激。

Control.java

  @JsonProperty(required = true) @NotNull(message = "isControl cannot be null") private Boolean isControl; public Boolean getIsControl() { return isControl; } @CheckBoolean(fieldName = "isControl") public void setIsControl(Boolean isControl) { this.isControl = isControl; } public class BooleanValidator implements ConstraintValidator { private String fieldName; @Override public void initialize(CheckBoolean constraintAnnotation) { this.fieldName = constraintAnnotation.fieldName(); } @Override public boolean isValid(Boolean value, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("The control flag %s should be either true or false", fieldName)) .addConstraintViolation(); if (value != null) { boolean isBoolean; if (value instanceof Boolean) { isBoolean = ((Boolean)value).booleanValue(); System.out.println("var isBoolean: " +isBoolean); return true; } else if (value instanceof Boolean && Boolean.FALSE.equals(value)) { isBoolean = ((Boolean)value).booleanValue(); return true; } else { return false; } } return false; } } 

例外:

假设您将布尔字段作为对象类型映射为HARDI,有两种方法可以做到这一点 –

1.自定义setter方法

  public class DTO { String key1; Object booleanKey; public Object getBooleanKey() { return booleanKey; } public void setBooleanKey(Object booleanKey) { if (booleanKey instanceof Boolean) { this.booleanKey = booleanKey; } else { // custom code here } } public String getKey1() { return key1; } public void setKey1(String key1) { this.key1 = key1; } } 

2.编写自定义解串器 –

 class BooleanKeyDeserializer extends JsonDeserializer { @Override public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { Object object = p.readValueAs(Object.class); if (!(object instanceof Boolean)) { // custom code here } return object; } } 

注释要为其执行自定义反序列化的字段 –

 class DTO { String key1; @JsonDeserialize(using = BooleanKeyDeserializer.class) Object booleanKey; //setters getters } 

我认为这是因为setter将其设置为boolean。 以下工作。 在这种情况下,我将布尔值作为反序列化类中的对象。

 public class Json { private String key1; private Object booleanKey; public String getKey1() { return key1; } public void setKey1(String key1) { this.key1 = key1; } public Object getBooleanKey() { return booleanKey; } public void setBooleanKey(Object booleanKey) { this.booleanKey = booleanKey; } } public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { String jsonString = "{\"key1\" : \"value1\", \"booleanKey\" : true}"; ObjectMapper mapper = new ObjectMapper(); Json obj = mapper.readValue(jsonString,Json.class); if (obj.getBooleanKey() instanceof Boolean) { System.out.println("booleanKey is boolean"); } else { System.out.println("booleanKey is some other beast"); } jsonString = "{\"key1\" : \"value1\", \"booleanKey\" : \"test\"}"; obj = mapper.readValue(jsonString, Json.class); if (obj.getBooleanKey() instanceof Boolean) { System.out.println("booleanKey is boolean"); } else { System.out.println("booleanKey is some other beast"); } }