Immutable Lombok annotated class with Jackson

后端 未结 9 2248
再見小時候
再見小時候 2020-12-25 13:02

What is the preferred way to create class that is

  • Immutable
  • Can be serialized/deserialized with Jackson
  • Human-readable and with low level of
9条回答
  •  南笙
    南笙 (楼主)
    2020-12-25 13:55

    By referencing answer by Joseph K. Strauss I came up with following solution.

    Plain lombok annotations that worked out for me looks like this. Following annotations give you immutable classes with builder that can be serialized and deserialized by Jackson.

        @Data
        @Setter(AccessLevel.NONE)
        @Builder(toBuilder = true)
        @AllArgsConstructor
        @NoArgsConstructor(access = AccessLevel.PRIVATE)
        public class Clazz {
            private String field;
        } 
    

    I prefer this solution because it requires no additional Jackson specific annotations and no additional lombok specific files

提交回复
热议问题