Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

Lombok - most popular usage combinations of Lombok annotations on DTO objects in java

0 contributions
11 points

Using with DTO object

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;

@Data
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class AddPostRequest {
    String postName;
    String postBody;
}

Why to use @NoArgsConstructor ?

We can use this DTO with json libraries that requires empty constructor. All fiels will be private and we have all args contructor annotation if we want to create object in this way.

Using with Hibernate entity

@Data
@AllArgsConstructor
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Entity
@Table(name = "posts")
public class PostsEntity {

    @Id
    @GeneratedValue
    Long id;
    String title;
	// ..
}

Lombok with logger - Slf4j

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LombokWithLogger {

    public static void main(String[] args) {
        log.info("Log info");
        log.error("Log error");
        
        // internally it uses:
        // org.slf4j
    }
}

References

  1. Lombok all features - stable
  2. Lombok all features - experimental
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more