Java @带有@Getter注释的JsonIgnore

Java @带有@Getter注释的JsonIgnore,java,json,jackson,lombok,Java,Json,Jackson,Lombok,我是否可以在不显式定义Getter的情况下将@JsonIgnore与lombok中的@Getter注释一起使用,因为我在序列化对象时必须使用此JsonIgnore,但在反序列化时,必须忽略JsonIgnore注释,以便对象中的字段不能为null @Getter @Setter public class User { private userName; @JsonIgnore private password; } 我知道,只要在password的getter上定义J

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

@Getter
@Setter
public class User {

    private userName;

    @JsonIgnore
    private password;
}
我知道,只要在password的getter上定义JsonIgnore,我就可以防止我的密码被序列化,但为此,我必须明确定义我不想要的getter。
如果您有任何想法,我们将不胜感激。

要将@JsonIgnore放到生成的getter方法中,您可以使用onMethod=@\uuu(@JsonIgnore)。这将生成带有特定注释的getter。有关更多详细信息,请查看


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

@Getter
@Setter
public class User {

    private userName;

    @Setter
    private password;

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

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

最近,我在使用jackson annotation 2.9.0和Lombok 1.18.2时也遇到了同样的问题

这就是我的工作原理:

@Getter
@Setter
public class User {

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

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

使用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

来源:

我最近也有同样的问题

有几种方法可以解决这个问题:

  • 在项目的根文件夹中创建文件
    lombok.config
    ,内容如下:
  • 在课堂上,您可以像往常一样在字段级别使用此注释:

    @JsonIgnore
    private String name;
    
    注意:如果您使用lombok@RequiredArgsConstructor或@AllArgsConstructor,则应使用
    @JsonIgnore
    删除
    @jsonignoreperties
    的所有用法(如解决方案4所述,或者您仍然可以选择解决方案2或3)。这是必需的,因为
    @JsonIgnore
    注释不适用于构造函数参数

  • 手动定义getter/setter并在其上添加
    @JsonIgnore
    注释:
  • 使用
    @JsonProperty
    (它可以是只读的,也可以是只读的,但不能同时使用这两种方式):
  • 使用
    @JsonIgnoreProperties({“fieldName1”、“fieldName2”和“…”})

  • 我个人在全局范围内使用solution#1,在类也有注释时使用solution#4作为异常处理
    @allargsconstuctor
    @requiredargsconstuctor

    ,但是如何使用呢?通过使用jsonIgnore和@getter,对象甚至不会反序列化?对我来说不起作用。可能是因为JDK版本。JavaDoc表示这取决于JDK版本:
    // says that it's primary config (lombok will not scan other folders then)
    config.stopBubbling = true
    
    // forces to copy @JsonIgnore annotation on generated constructors / getters / setters
    lombok.copyableAnnotations += com.fasterxml.jackson.annotation.JsonIgnore
    ...
    
    @JsonIgnore
    private String name;
    
    @JsonIgnore
    public String getName() { return name; }
    
    @JsonIgnore
    public void setName(String name) { this.name = name; }
    
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)  // will be ignored during serialization
    private String name;
    
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)  // will be ignored during deserialization
    private String name;