空 JSON 字段是 Java 模型中的 boolean/nullable 字段,正在转换为 null
An empty JSON field which is a boolean/nullable field in Java model, is getting converted as null
我有一个具体要求,
- 如果请求
JSON
body 中的(布尔类型)字段完全不存在,则请求有效。
- 如果这个字段设置了布尔值(
true
或false
),那么显然是有效的。
- 如果字段有 non-boolean 个值,它应该抛出异常。
- 如果字段有空值,它应该抛出异常。
即使 Java
模型通过类型检查在字段 non-boolean 时抛出异常,空值也会被转换为 null。相反,我需要能够区分它并抛出异常。如何实现?
这是我定义 post body 模型的方式,我使用的是 AutoValue 生成器,因此我没有手写字段的 setter 函数。另外,我正在使用 fasterxml 库。
@JsonProperty("indicator")
@Nullable
public abstract Boolean getIndicator();
我试过定义一个自定义注释并编写逻辑来检查该值是否为空,但没有成功。
您需要禁用 MapperFeature.ALLOW_COERCION_OF_SCALARS 功能,该功能允许在您遇到的情况下强制执行。请参阅以下示例:
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonApp {
public static void main(String[] args) throws Exception {
String json = "{\"indicator\":\"\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS);
System.out.println(mapper.readValue(json, BaseClass.class));
}
}
class BaseClass {
private Boolean indicator;
public Boolean getIndicator() {
return indicator;
}
public void setIndicator(Boolean indicator) {
this.indicator = indicator;
}
@Override
public String toString() {
return "BaseClass{" +
"indicator=" + indicator +
'}';
}
}
以上代码打印:
Exception in thread "main"
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot
coerce empty String ("") to Null value for type java.lang.Boolean
(enable MapperFeature.ALLOW_COERCION_OF_SCALARS
to allow) at
[Source: (String)"{"indicator":""}"; line: 1, column: 14] (through
reference chain: BaseClass["indicator"])
我有一个具体要求,
- 如果请求
JSON
body 中的(布尔类型)字段完全不存在,则请求有效。 - 如果这个字段设置了布尔值(
true
或false
),那么显然是有效的。 - 如果字段有 non-boolean 个值,它应该抛出异常。
- 如果字段有空值,它应该抛出异常。
即使 Java
模型通过类型检查在字段 non-boolean 时抛出异常,空值也会被转换为 null。相反,我需要能够区分它并抛出异常。如何实现?
这是我定义 post body 模型的方式,我使用的是 AutoValue 生成器,因此我没有手写字段的 setter 函数。另外,我正在使用 fasterxml 库。
@JsonProperty("indicator")
@Nullable
public abstract Boolean getIndicator();
我试过定义一个自定义注释并编写逻辑来检查该值是否为空,但没有成功。
您需要禁用 MapperFeature.ALLOW_COERCION_OF_SCALARS 功能,该功能允许在您遇到的情况下强制执行。请参阅以下示例:
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonApp {
public static void main(String[] args) throws Exception {
String json = "{\"indicator\":\"\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS);
System.out.println(mapper.readValue(json, BaseClass.class));
}
}
class BaseClass {
private Boolean indicator;
public Boolean getIndicator() {
return indicator;
}
public void setIndicator(Boolean indicator) {
this.indicator = indicator;
}
@Override
public String toString() {
return "BaseClass{" +
"indicator=" + indicator +
'}';
}
}
以上代码打印:
Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot coerce empty String ("") to Null value for type
java.lang.Boolean
(enableMapperFeature.ALLOW_COERCION_OF_SCALARS
to allow) at [Source: (String)"{"indicator":""}"; line: 1, column: 14] (through reference chain: BaseClass["indicator"])