布尔字段的 JSON Post 请求默认发送 false [英] JSON Post request for boolean field sends false by default
问题描述
我正在使用 FireFox RestClient 发送 JSON Post 请求.
Hi I am sending a JSON Post request using the FireFox RestClient.
我的 JSON 请求如下:
My JSON Request is as below:
{ "firstName": "Test", "lastName": "1", "isActive": 1 }
我的 POJO 有如下的 isActive 字段
My POJO has isActive field as below
private boolean isActive;
我的控制器定义如下
@RequestMapping(method = {RequestMethod.POST,
RequestMethod.PUT}, value = "/save")
public ResponseEntity<RestResponse> save(
@RequestBody POJOUserDetails userDetails, WebRequest request){
在我的 POJO 中,当我检查 isActive 的值时,无论我发送什么都是假的.我在我的 JSON 请求中尝试了以下值
In my POJO, when I check the value of isActive, it is false no matter what I send. I tried below value in my JSON request
"isActive": 1
"isActive": true
"isActive": "true"
"isActive": ""
"isActive": null
"isActive": false
以上所有内容都在我的控制器中发送错误.请帮忙.谢谢
All of above sends false in my controller. Please help. Thanks
添加 POJO 详细信息
Adding POJO details
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(include=Inclusion.NON_EMPTY)
public class POJOUserDetails {
private String firstName;
private String lastName;
private boolean isActive;
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
推荐答案
请记住,默认情况下,Jackson 从 getter 或 setter(第一个匹配的)确定属性名称.
Remember that Jackson, by default, determines the property name from either the getter or setter (the first that matches).
要反序列化 POJOUserDetails
类型的对象,Jackson 将查找三个属性
To deserialize an object of type POJOUserDetails
, Jackson will look for three properties
public void setFirstName(String firstName) {
public void setLastName(String lastName) {
public void setActive(boolean isActive) {
在 JSON 中.这些基本上是firstName
、lastName
、active
.
in the JSON. These are basically firstName
, lastName
, active
.
你得到以下 JSON
{ "firstName": "Test", "lastName": "1", "isActive": 1 }
因此 firstName
和 lastName
已映射,但您没有名为 isActive
的属性.
So firstName
and lastName
are mapped, but you don't have a property named isActive
.
Jackson 依赖于 Java Bean 命名约定及其访问器(getter)和修改器(setter).对于像
Jackson depends on Java Bean naming conventions with their accessors (getters) and mutators (setters). For a field like
private boolean isActive;
合适的 setter/getter 名称是
the appropriate setter/getter names are
public boolean getIsActive() {
return isActive;
}
public void setIsActive(boolean isActive) {
this.isActive = isActive;
}
所以你有两种可能的解决方案.如上所示更改您的 getter/setter 或使用 @JsonProperty
注释您的字段,以便 Jackson 使用字段名称来确定属性名称
So you have two possible solutions. Change your getter/setter as shown above or annotate your field with @JsonProperty
so that Jackson uses the field name to determine the property name
@JsonProperty
private boolean isActive;
这篇关于布尔字段的 JSON Post 请求默认发送 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!