gpt4 book ai didi

java - Spring MVC Rest Service JSON - 将 boolean 值的默认值设置为 true

转载 作者:行者123 更新时间:2023-11-30 02:41:31 24 4
gpt4 key购买 nike

boolean的默认值设置为true

这是 dto 类,其中包含 boolean 值。

DTO 类

public class SensitivityDto extends AuditableEntity implements Serializable {
private static final long serialVersionUID = 1L;


private long sensitivityId;
@JsonIgnore
private boolean isSelected;

public SensitivityDto() {
}
/** other getter/setters */
public boolean isIsSelected() {
return isSelected;
}

public void setIsSelected(boolean isSelected) {
this.isSelected = isSelected;
}

}

输入json

{
"sensitivity": {
"sensitivityId": 100,
"isSelected": "true", // if not passing this field always its treated as null.

}
}

Controller

public @ResponseBody ResultDecorator saveLabResultCultureDetails(@RequestBody SensitivityDto sensitivityDto) throws  Exception {

}

如何将 boolean 值默认设置为 true,这样,如果 json 中不存在该值,那么它不应该为 false,而应该为 true。

最佳答案

什么?

@JsonIgnore
private boolean isSelected = true;

来自评论:

then even I'm sending false in json, its takng as true

您必须删除@JsonIgnore并使用@JsonAutoDetect ,如下:

import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class SensitivityDto extends AuditableEntity implements Serializable {
private static final long serialVersionUID = 1L;

private long sensitivityId;


private boolean isSelected = true;

public SensitivityDto() {
}

/** other getter/setters */
public boolean isIsSelected() {
return isSelected;
}

public void setIsSelected(boolean isSelected) {
this.isSelected = isSelected;
}

}

关于java - Spring MVC Rest Service JSON - 将 boolean 值的默认值设置为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41531136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com