【问题标题】:Can't use lombok @NoArgsConstructor on a class不能在类上使用 lombok @NoArgsConstructor
【发布时间】:2018-07-31 07:45:41
【问题描述】:

我将 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;
}

可能是什么原因?

【问题讨论】:

  • 你可以试试@NoArgsConstructor 没有@Builder 吗?
  • 它可以在没有@Builder 的情况下工作,但事实是我真的需要这个。
  • 您使用的是哪个版本的 lombok
  • 如果你省略@NoArgsConstructor并自己添加默认构造函数,它是否有效?
  • 如果使用构建器。您需要添加@AllArgsConstructor。所以你需要两者。 '@AllArgsConstructor' '@NoArgsConstructor'

标签: java jackson lombok


【解决方案1】:

@AllArgsConstructor@NoArgsConstructor 注释添加到您的类中

【讨论】:

    【解决方案2】:

    这是 lombok 版本 1.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 {
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-23
      • 1970-01-01
      • 2021-09-19
      • 2019-09-24
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      相关资源
      最近更新 更多