Skip to content

DtoConvertible

The DTOConvertible interface allows to convert a complete entity object into the related Data Transfer Object

Usage

Entity Class Example

public class CarEntity {

    private final String id;

    private final String name;

    public CarEntity(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

}
class CarEntity(
    val id: String?,
    val name: String?
)  {

}
@DTO
public class CarEntityDto {

    private final String name;

    public CarEntityDto(String id, String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}
@DTO
data class CarEntityDto(
    val name: String?
)

Implementation

Make implement to the entity class the DTOConvertible annotation to override the convertToRelatedDTO method for the mapping conversion

public class CarEntity implements DTOConvertible<CarEntityDto> {

    ...

    @Override
    public CarEntityDto convertToRelatedDTO() {
        return new CarEntityDto(name);
    }

}
class CarEntity(
    val id: String?,
    val name: String?
) : DTOConvertible<CarEntityDto?> {

    override fun convertToRelatedDTO(): CarEntityDto {
        return CarEntityDto(name)
    }
}

Conversion

Invoke the convertToRelatedDTO method to obtain the DTO object

public CarEntityDto getCarDTO() {
    CarEntity carEntity = new CarEntity("1", "Ferrari");

    return carEntity.convertToRelatedDTO();
}
fun getCarDTO(): CarEntityDto {
    val carEntity = CarEntity("1", "Ferrari")

    return carEntity.convertToRelatedDTO()
}