Java @JsonIgnore 和 @Getter 注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24466464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:22:11  来源:igfitidea点击:

@JsonIgnore with @Getter Annotation

javajsonHymansonlombok

提问by Prateek Jain

Can i use @JsonIgnore with @Getter annotation from lombok without explicitly define the getter, because i have to use this JsonIgnore while serializing the object but while deserializing, the JsonIgnore annotation must be ignore so the field in my object must not be a null.

我可以在不显式定义 getter 的情况下将 @JsonIgnore 与来自 lombok 的 @Getter 注释一起使用吗,因为我必须在序列化对象时使用此 JsonIgnore,但在反序列化时,必须忽略 JsonIgnore 注释,因此我的对象中的字段不能为空。

@Getter
@Setter
public class User {

    private userName;

    @JsonIgnore
    private password;
}

i know, just by define the JsonIgnore on the getter of passwordi can prevent my password to be serialized but for that i have to explicitly define the getter thing that i don't want. Any idea please, Any help will be appreciated.

我知道,只需在密码的 getter 上定义 JsonIgnore,我就可以防止我的密码被序列化,但为此我必须明确定义我不想要的 getter 内容。请有任何想法,任何帮助将不胜感激。

采纳答案by Sebastian

To put the @JsonIgnore to the generated getter method, you can use onMethod = @__( @JsonIgnore ). This will generate the getter with the specific annotation. For more details check http://projectlombok.org/features/GetterSetter.html

要将 @JsonIgnore 放入生成的 getter 方法,您可以使用 onMethod = @__( @JsonIgnore )。这将生成具有特定注释的 getter。有关更多详细信息,请查看 http://projectlombok.org/features/GetterSetter.html

@Getter
@Setter
public class User {

    private userName;

    @Getter(onMethod = @__( @JsonIgnore ))
    @Setter
    private password;
}

回答by ZooMMX

This could be quite obvious but I lost a lot of time not thinking this solution before:

这可能很明显,但我之前浪费了很多时间没有考虑这个解决方案:

@Getter
@Setter
public class User {

    private userName;

    @Setter
    private password;

    @JsonIgnore
    public getPassword() { return password; }
}

As Sebastian said @__( @JsonIgnore )can resolve this issue but sometimes the use of the onX Lombok feature (@__()) can have side-effects for example breaking the javadoc generation.

正如 Sebastian 所说@__( @JsonIgnore )可以解决这个问题,但有时使用 onX Lombok 功能(@__())会产生副作用,例如破坏 javadoc 生成。

回答by damato

Recently i had the same issue using Hymanson-annotation 2.9.0 and lombok 1.18.2

最近我在使用 Hymanson-annotation 2.9.0 和 lombok 1.18.2 时遇到了同样的问题

This is what worked for me:

这对我有用:

@Getter
@Setter
public class User {

    @JsonIgnore
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;

So basically adding the annotation @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)means that the property may only be written for deserialization (using setter) but will not be read on serialization (using getter)

所以基本上添加注释@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)意味着该属性可能只为反序列化而写入(使用 setter),而不会在序列化时读取(使用 getter)

回答by Ali786

With JDK version 8 use below:

使用 JDK 版本 8 如下:

//  @Getter(onMethod=@__({@Id, @Column(name="unique-id")})) //JDK7
//  @Setter(onParam=@__(@Max(10000))) //JDK7
 @Getter(onMethod_={@Id, @Column(name="unique-id")}) //JDK8
 @Setter(onParam_=@Max(10000)) //JDK8

Source : https://projectlombok.org/features/experimental/onX

来源:https: //projectlombok.org/features/experimental/onX