Jackson renames primitive boolean field by removing 'is'

后端 未结 10 830
渐次进展
渐次进展 2020-11-27 12:07

This might be a duplicate. But I cannot find a solution to my Problem.

I have a class

public class MyResponse implements Serializable {

    private          


        
10条回答
  •  臣服心动
    2020-11-27 12:37

    Building upon Utkarsh's answer..

    Getter names minus get/is is used as the JSON name.

    public class Example{
        private String radcliffe; 
    
        public getHarryPotter(){
            return radcliffe; 
        }
    }
    

    is stored as { "harryPotter" : "whateverYouGaveHere" }


    For Deserialization, Jackson checks against both the setter and the field name. For the Json String { "word1" : "example" }, both the below are valid.

    public class Example{
        private String word1; 
    
        public setword2( String pqr){
            this.word1 = pqr; 
        }
    }
    
    public class Example2{
        private String word2; 
    
        public setWord1(String pqr){
            this.word2 = pqr ; 
        }
    }
    

    A more interesting question is which order Jackson considers for deserialization. If i try to deserialize { "word1" : "myName" } with

    public class Example3{
        private String word1;
        private String word2; 
    
        public setWord1( String parameter){
            this.word2 = parameter ; 
        }
    }
    

    I did not test the above case, but it would be interesting to see the values of word1 & word2 ...

    Note: I used drastically different names to emphasize which fields are required to be same.

提交回复
热议问题