keepmoving
keepmoving

Reputation: 2043

JsonIgnore annotation is not working with Lombok

I am facing really weird issue with Lombok and Jackson. Following of piece of code on which I am working.

@Getter
@Setter
@NoArgsConstructor
@XmlRootElement
public class Order{

    //@JsonIgnore
    @Getter(onMethod = @__(@JsonIgnore))
    private boolean userPresent;
}

So what I want , this dto supposed to serialized as Json then this userPresent attribute should not come as response attribute. I though @JsonIgnore will work for me. But I think it as some issue with Lombok as per https://stackoverflow.com/a/57119494/2111677 article. Then I changed the approach to use OnMethod.

Now , on eclipse compiling perfectly fine but when I am trying to compile using mvn then it gives me following error.

enter image description here

Could someone help me fix when its not working with maven.

Upvotes: 2

Views: 5053

Answers (3)

Jat Torres
Jat Torres

Reputation: 321

This worked for me.

@RequiredArgsConstructor
@Setter
@NoArgsConstructor
@Entity
public class ProductColor {

    @Id
    @GeneratedValue
    @NonNull
    @Getter
    private Long id;

    @NonNull
    @Getter
    private String color;

    @ManyToMany(mappedBy="colors")
    @Getter(onMethod_ = @JsonIgnore)
    private Set<Product> products;

}

Upvotes: 1

luisZavaleta
luisZavaleta

Reputation: 1168

It does not work, a workaround is using @JsonIgnoreProperties at class level:

@JsonIgnoreProperties({"email"})

Upvotes: 2

Jan Rieke
Jan Rieke

Reputation: 8042

The @__ style is for javac7. For javac8+ you have to use this variant:

@Getter(onMethod_=@JsonIgnore)

However, it is sufficient to have the @JsonIgnore annotation on either the field, the getter, or the setter. If it is present on at least one of those, the whole "virtual property" is ignored completely during (de-)serialization. So if that is what you want, you don't need that onMethod_.

If you want it to be ignored only during serialization, but not on deserialization, you have to add a @JsonProperty on the setter:

@JsonIgnore
@Setter(onMethod_=@JsonProperty)
private boolean userPresent;

Upvotes: 7

Related Questions