CurrencyDTO – Lombok Usage Guide
This document explains how to use Lombok annotations (@Builder, @AllArgsConstructor, @Getter, @Setter) with the CurrencyDTO class and shows alternatives without Lombok.
DTO Definition
package wallet.api.money.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@Builder
public class CurrencyDTO {
private String fromCurrency;
private String toCurrency;
private Double amount;
} A DTO (Data Transfer Object) is used to transfer data between layers such as controller, service, and client. It should not contain business logic.
1. Usage with @Builder
@Builder makes object creation more readable and safer. It is especially useful for DTOs with multiple fields.
CurrencyDTO dto = CurrencyDTO.builder()
.fromCurrency("USD")
.toCurrency("EUR")
.amount(100.0)
.build(); 2. Usage with @AllArgsConstructor
@AllArgsConstructor generates a constructor that includes all fields.
CurrencyDTO dto = new CurrencyDTO("USD", "EUR", 100.0); 3. Usage Without Lombok
When Lombok is not used, all boilerplate code must be written manually.
public class CurrencyDTO {
private String fromCurrency;
private String toCurrency;
private Double amount;
public CurrencyDTO() {
}
public CurrencyDTO(String fromCurrency, String toCurrency, Double amount) {
this.fromCurrency = fromCurrency;
this.toCurrency = toCurrency;
this.amount = amount;
}
public String getFromCurrency() {
return fromCurrency;
}
public void setFromCurrency(String fromCurrency) {
this.fromCurrency = fromCurrency;
}
public String getToCurrency() {
return toCurrency;
}
public void setToCurrency(String toCurrency) {
this.toCurrency = toCurrency;
}
public Double getAmount() {
return amount;
}
public void setAmount(Double amount) {
this.amount = amount;
}
} Usage:
CurrencyDTO dto = new CurrencyDTO();
dto.setFromCurrency("USD");
dto.setToCurrency("EUR");
dto.setAmount(100.0);