Im new to Spring and Im trying to create DTO with Lombok @Builder.
But i having trouble set up the data from field class
take a look at addressDTO
and childsDTO
in ParentDTO
class.
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Parent {
private String name;
private Integer age;
private Address address;
private List<Child> childs;
}
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Child {
private String name;
private Integer age;
}
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Address {
private Integer Zipcode;
private String City;
}
DTO class
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class ParentDTO {
private String nameDTO;
private Integer ageDTO;
private AddressDTO addressDTO;
private List<ChildDTO> childsDTO;
public static ParentDTO convertParentDTO(Parent parent) {
return ParentDTO.builder()
.nameDTO(parent.getName())
.ageDTO(parent.getAge())
.addressDTO( null) // i try this wont work .addressDTO(parent.getAddress()) .addressDTO( AddressDTO::connerAddressDTO))
.childsDTO(null) // i try this .childsDTO(List.of()) then ?
.build();
}
}
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class ChildDTO {
private String nameDTO;
private Integer ageDTO;
public static ChildDTO convertChildDTO(Child child) {
return ChildDTO.builder()
.nameDTO(child.getName())
.ageDTO(child.getAge())
.build();
}
}
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class AddressDTO {
private Integer ZipcodeDTO;
private String CityDTO;
public static AddressDTO connerAddressDTO(Address address) {
return AddressDTO.builder()
.ZipcodeDTO(address.getZipcode())
.CityDTO(address.getCity())
.build();
}
}
CodePudding user response:
If I understand well the question it's should be like this.
import lombok.*;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class ParentDTO {
private String nameDTO;
private Integer ageDTO;
private AddressDTO addressDTO;
private List<ChildDTO> childsDTO;
public static ParentDTO convertParentDTO(Parent parent) {
return ParentDTO.builder()
.nameDTO(parent.getName())
.ageDTO(parent.getAge())
.addressDTO(
AddressDTO.connerAddressDTO(
parent.getAddress()))
.addressDTO(parent.getAddress())
.childsDTO(toChildrenDto(parent.getChilds()))
.build();
}
private static List<ChildDTO> toChildrenDto(List<Child> children) {
if (children == null || children.isEmpty()) {
return Collections.emptyList();
}
return children.stream()
.map(c ->
ChildDTO.builder().ageDTO(c.getAge()).nameDTO(c.getName()).build())
.collect(Collectors.toList());
}
}
CodePudding user response:
For .addressDTO it's probably AddressDTO.connerAddressDTO(parent.getAddress())
.
For .childsDTO parent.getChilds().stream().map(ChildDTO::convertChildDTO).collect(Collectors.toList())
or change convertChildDTO parameter to receive List<Child>.