Languages

Lombok - what is the best way to omit one filed Setter/Getter in Lombok?

8 points
Asked by:
Jax
388

I am looking for the best way to omit one field setter/getter in Lombok.

In my case all getters are defined on the entire class level. I am looking for annotation that is able to disable automatic generating getter and setter only for specific field.

Is it possible?

My source code:

package com.gallery.requests;

import lombok.Getter;
import lombok.Setter;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ItemRequest
{
    private String name;
    private String description;
    private Long owner;  // <---------------------- I don't want to have generated getters and setters for this field.

    // ...
}

 

1 answer
3 points
Answered by:
Jax
388

You should add to single field the following annotations:

// import lombok.AccessLevel;

@Getter(AccessLevel.NONE)  // <--------------- the solution
@Setter(AccessLevel.NONE)  // <--------------- the solution
private Long owner;

 

Practical example:

package com.gallery.requests;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ItemRequest
{
    private String name;
    private String description;

    @Getter(AccessLevel.NONE)  // <--------------- the solution
    @Setter(AccessLevel.NONE)  // <--------------- the solution
    private Long owner;

    // ...
}

 

See also

  1. Java - omitting one Setter/Getter in Lombok

0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join