不能在 class 上使用 lombok @NoArgsConstructor

Can't use lombok @NoArgsConstructor on a class

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

这是我的 class:

@Getter
@Builder
public class AccountItem {

/**
 * Accounti dentifier
 */
private Long accountId;

/**
 * Account name
 */
private String accountName;

/**
 * Account priority
 */
private int accountPriority;
}

可能是什么原因?

这是 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 {
    }
}

@AllArgsConstructor@NoArgsConstructor 注释添加到您的 class