Lombok的lombok.copyableAnnotations无法与Jackson批注一起使用 [英] Lombok's lombok.copyableAnnotations not working with Jackson annotations

查看:163
本文介绍了Lombok的lombok.copyableAnnotations无法与Jackson批注一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Lombok的新功能copyableAnnotations,以便将@JsonIgnore@JsonValue之类的Jackson注释复制到生成的getter/wither方法中.该博客似乎暗示这应该可行: https://www.thecuriousdev.org/lombok-builder-with-jackson/.但是,当我尝试这样做时,我只会得到错误:不适用于这种声明的注释类型"(指向我的value字段).为什么这不起作用,我如何使它起作用?也许我误会了该功能的工作原理.我正在使用lombok 1.18.8.

I'm trying to make use of Lombok's new copyableAnnotations feature in order to have Jackson annotations like @JsonIgnore and @JsonValue copied to generated getter/wither methods. This blog seems to suggest this should work: https://www.thecuriousdev.org/lombok-builder-with-jackson/. However, when I try this I simply get "error: annotation type not applicable to this kind of declaration" (pointing to my value field). Why is this not working and how do I make it work? Perhaps I'm misunderstanding how this feature is supposed to work. I'm using lombok 1.18.8.

model.java:

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Value;

import javax.validation.constraints.NotNull;

@Value
public class BrandId implements ValueObject<Long> {

    @JsonCreator
    public static BrandId of(final Long value) {

        return new BrandId(value);
    }

    @NotNull
    @JsonValue
    private Long value;
}

lombok.config:

config.stopBubbling = true

lombok.copyableAnnotations += com.fasterxml.jackson.annotation.JsonIgnore
lombok.copyableAnnotations += com.fasterxml.jackson.annotation.JsonProperty
lombok.copyableAnnotations += com.fasterxml.jackson.annotation.JsonValue

推荐答案

为什么这不起作用[…]?

Why is this not working […] ?

仅在方法声明和其他注释类型的声明上允许使用@JsonValue注释;因此,无论是否使用龙目岛,您都无法将其放在田野上. (如果您查看其Javadoc ,您会看到它带有@Target(value={ANNOTATION_TYPE,METHOD})注释.)

The @JsonValue annotation is only allowed on method declarations and on declarations of other annotation types; so, with or without Lombok, you can't put it on a field. (If you look at its Javadoc, you'll see that it's annotated with @Target(value={ANNOTATION_TYPE,METHOD}).)

好消息是@JsonValue仅适用于getter方法(不适用于setter方法,builder方法等),每个类只能有一个,因此手动创建它并没什么大不了的一个吸气剂:

The good news is that @JsonValue only applies to getter methods (not setter methods, builder methods, etc.), and there can be only one of it per class, so it's not a big deal to just manually create that one getter:

    @NotNull
    private Long value;

    @JsonValue
    public Long getValue() {
        return value;
    }

如果您真的不喜欢它,则可以使用Lombok的实验性onMethod功能:

If you really dislike that, then you can use Lombok's experimental onMethod feature:

    @NotNull
    @Getter(onMethod=@__({@JsonValue}))
    private Long value;

与上述内容相同,只是处于实验阶段(因此在将来的Lombok和/或Java版本中可能会更改或消失).

which is equivalent to the above except in being experimental (so it may change or disappear in future versions of Lombok and/or Java).

这篇关于Lombok的lombok.copyableAnnotations无法与Jackson批注一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆