Fasterxml Jackson自动将非布尔值转换为布尔值 [英] Fasterxml Jackson automatically converts non-boolean value to a boolean value

查看:170
本文介绍了Fasterxml Jackson自动将非布尔值转换为布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a pojo class where one of the flag isControl which is of type Boolean.

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

When this property gets a non boolean value other than true or false fasterxml jackson automatically converts the input value to true. After debugging for few hours I find out that this is happening in the setter method setIsControl.

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

I want to pass a custom message if input value for this property is non-boolean. I have written my own annotation to validate the input value for this property and return custom message if its not a boolean value but jackson binds the value before checking my custom validator.

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

Using jackson version >>> 2.6.3. Any help will be appreciated.

Control.java

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

异常:

推荐答案

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

There are two ways of doing this assuming that you will map boolean field as Object type as HARDI answered -

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

注释要执行自定义反序列化的字段 -

Annotate the field for which you want to perform custom deserialization -

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

这篇关于Fasterxml Jackson自动将非布尔值转换为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆