【问题标题】:@JsonIgnore with @Getter Annotation@JsonIgnore 和 @Getter 注解
【发布时间】:2014-08-19 10:16:14
【问题描述】:

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

@Getter
@Setter
public class User {

    private userName;

    @JsonIgnore
    private password;
}

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

【问题讨论】:

  • 但是如何?通过使用带有@getter 的jsonIgnore 对象甚至不会反序列化?

标签: java json jackson lombok


【解决方案1】:

要将@JsonIgnore 放到生成的getter 方法中,可以使用onMethod = @__( @JsonIgnore )。这将生成带有特定注释的 getter。更多详情请查看 http://projectlombok.org/features/GetterSetter.html

@Getter
@Setter
public class User {

    private userName;

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

【讨论】:

  • 对我不起作用。可能是因为JDK版本。 JavaDoc 说它取决于 JDK 版本:
  • 如果你想防御,你应该用 ignore-getter 注释类,并用不被忽略的 getter 启用可见字段。
【解决方案2】:

最近我在使用 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)

【讨论】:

    【解决方案3】:

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

    @Getter
    @Setter
    public class User {
    
        private userName;
    
        @Setter
        private password;
    
        @JsonIgnore
        public getPassword() { return password; }
    }
    

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

    【讨论】:

      【解决方案4】:

      我最近遇到了同样的问题。

      有几种方法可以解决:

      1. 在项目的根文件夹中创建文件lombok.config,内容为:
      // 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;
      

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

      1. 手动定义 Getter / Setter + 添加 @JsonIgnore 注释:
      @JsonIgnore
      public String getName() { return name; }
      
      @JsonIgnore
      public void setName(String name) { this.name = name; }
      
      1. 使用@JsonProperty(只读或只写,但不能同时使用):
      @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;
      
      1. 使用@JsonIgnoreProperties({ "fieldName1", "fieldName2", "..."})

      当类也有注释 @AllArgsConstructor@RequiredArgsConstructor 时,我个人在全局范围内使用解决方案 #1 和解决方案 #4。

      【讨论】:

        【解决方案5】:

        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
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-31
          • 1970-01-01
          • 1970-01-01
          • 2018-10-13
          • 2018-12-08
          • 1970-01-01
          • 2020-07-03
          • 2019-08-21
          相关资源
          最近更新 更多