代码之家  ›  专栏  ›  技术社区  ›  Maciaz

不能在类上使用lombok@NoArgsConstructor

  •  2
  • Maciaz  · 技术社区  · 6 年前

    我使用Jackson作为Json解析器,得到一个错误:

    No suitable constructor found for type ~ can not instantiate from JSON object
    

    所以我试着加上 @NoArgsConstructor ,但现在我得到了这个:

    constructor AccountItem in class AccountItem cannot be applied to given types
    

    这是我的课:

    @Getter
    @Builder
    public class AccountItem {
    
    /**
     * Accounti dentifier
     */
    private Long accountId;
    
    /**
     * Account name
     */
    private String accountName;
    
    /**
     * Account priority
     */
    private int accountPriority;
    }
    

    可能是什么原因?

    2 回复  |  直到 6 年前
        1
  •  3
  •   Jean-François Fabre    5 年前

    两者相加 @AllArgsConstructor @NoArgsConstructor 对类的注释

        2
  •  1
  •   rechandler    6 年前

    这是Lombok1.16.20版本中的已知问题。 https://github.com/rzwitserloot/lombok/issues/1563

    你可以这样做:

    @Getter
    @Builder
    @JsonDeserialize(builder = AccountItem.AccountItemBuilder.class)
    public class AccountItem {
    
        /**
         * Accounti dentifier
         */
        private Long accountId;
    
        /**
         * Account name
         */
        private String accountName;
    
        /**
         * Account priority
         */
        private int accountPriority;
    
        @JsonPOJOBuilder(withPrefix = "")
        public static final class AccountItemBuilder {
        }
    }