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

Fasterxml Jackson automatically converts non-boolean value to a boolean value

我有一个 pojo class,其中一个标志 isControl 是布尔类型。

当此 属性 获得除 true or false 以外的非布尔值时,fasterxml jackson 会自动将输入值转换为 true。调试了几个小时后,我发现这是在 setter 方法 setIsControl.

中发生的

如果此 属性 的输入值为非布尔值,我想传递自定义消息。我已经编写了自己的注释来验证此 属性 和 return 自定义消息的输入值(如果它不是布尔值但 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<CheckBoolean,  Boolean> {

    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;
}
}

异常:

我认为这是因为 setter 将其设置为布尔值。以下作品。在这种情况下,我在反序列化 class.

中将布尔值作为对象
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");
        }
    }

假设您将布尔字段映射为 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<Object> {

@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
}