Java 布尔字段的 JSON Post 请求默认发送 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21913955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:23:30  来源:igfitidea点击:

JSON Post request for boolean field sends false by default

javajsonspring-mvcHymansonpojo

提问by NewQueries

Hi I am sending a JSON Post request using the FireFox RestClient.

嗨,我正在使用 FireFox RestClient 发送 JSON Post 请求。

My JSON Request is as below:

我的 JSON 请求如下:

 { "firstName": "Test", "lastName": "1", "isActive": 1 }

My POJO has isActive field as below

我的 POJO 有 isActive 字段如下

  private boolean isActive;

My Controller is define as below

我的控制器定义如下

@RequestMapping(method = {RequestMethod.POST, 
                                 RequestMethod.PUT}, value = "/save")
public ResponseEntity<RestResponse> save(
      @RequestBody POJOUserDetails userDetails, WebRequest request){

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

在我的 POJO 中,当我检查 isActive 的值时,无论我发送什么都是假的。我在 JSON 请求中尝试了以下值

"isActive": 1
"isActive": true 
"isActive": "true"
"isActive": ""
"isActive": null
"isActive": false

All of above sends false in my controller. Please help. Thanks

以上所有内容在我的控制器中发送错误。请帮忙。谢谢

Adding POJO details

添加 POJO 详细信息

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

采纳答案by Sotirios Delimanolis

Remember that Hymanson, by default, determines the property name from either the getter or setter (the first that matches).

请记住,默认情况下,Hymanson 从 getter 或 setter(第一个匹配的)确定属性名称。

To deserialize an object of type POJOUserDetails, Hymanson will look for three properties

为了反序列化类型为 的对象POJOUserDetails,Hymanson 将查找三个属性

public void setFirstName(String firstName) {

public void setLastName(String lastName) {

public void setActive(boolean isActive) {

in the JSON. These are basically firstName, lastName, active.

在 JSON 中。这些基本上firstNamelastName,,active

You get the following JSON

你得到以下 JSON

{ "firstName": "Test", "lastName": "1", "isActive": 1 }

So firstNameand lastNameare mapped, but you don't have a property named isActive.

因此firstNamelastName已映射,但您没有名为isActive.

Hymanson depends on Java Bean naming conventions with their accessors (getters) and mutators (setters). For a field like

Hymanson 依赖于 Java Bean 命名约定及其访问器(getter)和修改器(setter)。对于像这样的领域

private boolean isActive;

the appropriate setter/getter names are

适当的 setter/getter 名称是

public boolean getIsActive() {
    return isActive;
}

public void setIsActive(boolean isActive) {
    this.isActive = isActive;
}

So you have two possible solutions. Change your getter/setter as shown above or annotate your field with @JsonPropertyso that Hymanson uses the field name to determine the property name

所以你有两种可能的解决方案。如上所示更改您的 getter/setter 或注释您的字段,@JsonProperty以便 Hymanson 使用字段名称来确定属性名称

@JsonProperty
private boolean isActive;

回答by Chinmay Biswal

When you are using libraries like lombok to generate getters and setters, don't add 'is' with the field name if the field type is boolean. Because Hymanson uses default naming bean convention of java and adds 'is' while setting fields. so adding 'is' makes the field mapping wrong

当您使用 lombok 之类的库生成 getter 和 setter 时,如果字段类型为布尔值,请不要在字段名称中添加“is”。因为 Hymanson 使用 java 的默认命名 bean 约定并在设置字段时添加“is”。所以添加'is'会使字段映射错误

回答by Omar

In the other hand, we can use Booleanwrapper to handle it, this way the IDE generates the getter/setter by adding the prefix get/setto the whole Boolean field's name.

另一方面,我们可以使用Boolean包装器来处理它,这样 IDE 通过将前缀添加get/set到整个布尔字段的名称来生成 getter/setter 。