java - @JsonIgnore 不在不同的环境中工作
问题描述
这是我使用@JsonIgnore 的bean 类。我的测试用例通过了,我在本地环境中的 JSON 中看不到 catColor,但在开发环境中,我仍然能够在 JSON 中看到 catColor。如果我必须对代码进行任何更改,或者我应该检查环境中的任何内容,请告诉我。
JSON:
{"catName":"","catEyes":"","catDetails":"","catColor":"" }
猫豆.java
import com.fasterxml.jackson.annotation.JsonIgnore;
@XmlRootElement
@SuppressWarnings("PMD")
public class CatBean {
private String catName;
private String catEyes;
private String catDetails;
public String getCatName(){
return catName;
}
public void setCatName(String catName){
this.catName=catName;
}
public String getCatEyes(){
return catEyes;
}
public void setCatEyes(String catEyes){
this.catEyes=catEyes;
}
public String getCatDetails(){
return catDetails;
}
public void setCatDetails(String catDetails){
this.catDetails=catDetails;
}
@JsonIgnore
public String getCatColor() {
return getCatDetails(catColor);
}
}
CatDetails.java
public class CatDetails {
private String name;
private String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
build.gradle ( Gradle imports)
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.9.1'
compile 'com.fasterxml.jackson.core:jackson-core:2.9.9'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.9'
我没有定义为的字段catColor
。我在本地和开发中都在Java 7
使用。Gradle 4.10.3
它在本地工作,但在开发中不起作用。我写了一个 JUNIT 测试用例来测试 JSON。
(用于检查正在创建的 JSON 的测试用例)
@Test
public void testJsonResponseNotContainingCardPresent() throws HostAuthException, ISOParseException, ParseException, IOException {
CatBean catBean= new CatBean();
ObjectMapper mapper = new ObjectMapper();
String catBeanAsString = Mapper.writeValueAsString(catBean);
assertThat(requestBeanAsString,not(containsString("catColor")));
}
解决方案
推荐阅读
- python - 无法在 python2.7 上安装 scypt
- c# - 对象 null POST 方法 Web API
- java - 使用 json-to-xml() 函数的 json 到 xml 转换会产生不合需要格式的输出
- c - 如何在C中设置变量的最大和最小限制?
- ocaml - 为什么 ocaml 中函数定义的替代方案会给出不同的结果?
- c++ - 如何在 C++ 中将整数数组向量转换为二维数组?
- java - ExpandableListview :想在每个组下添加子列表延迟加载
- mysql - Hibernate 是否验证临时表?
- drupal-8 - 以编程方式更改批量表单操作选项
- wordpress - 如何通过静态 VPS IP 访问 Wordpress