首页 > 解决方案 > @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")));

    }


标签: javajsonspringjackson

解决方案


推荐阅读