Jackson renames primitive boolean field by removing 'is'

后端 未结 10 804
渐次进展
渐次进展 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:56

    This is a slightly late answer, but may be useful for anyone else coming to this page.

    A simple solution to changing the name that Jackson will use for when serializing to JSON is to use the @JsonProperty annotation, so your example would become:

    public class MyResponse implements Serializable {
    
        private boolean isSuccess;
    
        @JsonProperty(value="isSuccess")        
        public boolean isSuccess() {
            return isSuccess;
        }
    
        public void setSuccess(boolean isSuccess) {
            this.isSuccess = isSuccess;
        }
    }
    

    This would then be serialised to JSON as {"isSuccess":true}, but has the advantage of not having to modify your getter method name.

    Note that in this case you could also write the annotation as @JsonProperty("isSuccess") as it only has the single value element

提交回复
热议问题