Spring Boot Mapstruct StackOverFlow错误

Fel*_* A. 4 java spring mapstruct

我正在使用mapstruct映射我的实体和dto类...我在mapper类上存在循环问题...

我没有意识形态该怎么办...这是我的映射器课程

    @Mapper(componentModel = "spring", uses = {BrandMapper.class})
public interface VehicleTypeMapper {

    VehicleTypeDTO vehicleTypetoVehicleTypeDTO(VehicleType vehicleType);

    Iterable<VehicleTypeDTO> vehicleTypetoVehicleTypeDTO(Iterable<VehicleType> vehicleTypes);

    VehicleType vehicleTypeDTOtoVehicleType(VehicleTypeDTO vehicleTypeDTO);
}

    @Mapper(componentModel = "spring", uses = { VehicleTypeMapper.class, ModelMapper.class })
public interface BrandMapper {

    BrandDTO brandtoBrandDTO(Brand brand);

    Iterable<BrandDTO> brandtoBrandDTO(Iterable<Brand> brands);

    Brand brandDTOtoBrand(BrandDTO brandDTO);
}
Run Code Online (Sandbox Code Playgroud)

我的实体类... DTO与我的实体类具有相同的属性...

@Entity
@Table(name = "tb_brand")
public class Brand implements Serializable {

    private static final long serialVersionUID = 1506494747401320985L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "vehicle_type_id", foreignKey = @ForeignKey(name = "fk_vehicle_type"))
    private VehicleType vehicleType;

    @JsonIgnore
    @OneToMany(mappedBy = "brand", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Model> models;

    @Column(name = "description", nullable = false)
    private String description;

//GETS AND SETS
}

@Entity
@Table(name = "tb_vehicle_type")
public class VehicleType {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @JsonIgnore
    @OneToMany(mappedBy = "vehicleType", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Brand> brands;

    @Column(name = "description", nullable = false)
    private String description;

//GETS AND SETS
}
Run Code Online (Sandbox Code Playgroud)

堆栈轨迹

at br.com.meuveiculocerto.business.mapper.VehicleTypeMapperImpl.brandListToBrandDTOList(VehicleTypeMapperImpl.java:81) ~[classes/:na]
at br.com.meuveiculocerto.business.mapper.VehicleTypeMapperImpl.vehicleTypetoVehicleTypeDTO(VehicleTypeMapperImpl.java:33) ~[classes/:na]
at br.com.meuveiculocerto.business.mapper.BrandMapperImpl.brandtoBrandDTO(BrandMapperImpl.java:35) ~[classes/:na]
at br.com.meuveiculocerto.business.mapper.VehicleTypeMapperImpl.brandListToBrandDTOList(VehicleTypeMapperImpl.java:81) ~[classes/:na]
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我确定为什么循环吗?

Fil*_*lip 5

VehicleType和之间存在循环依赖关系Brand。您有3种可能性来解决周期:

  1. 一个映射器将始终忽略循环字段。我看到您@JsonIgnore在的清单BrandVehicleType。您可以通过Mapping#ignore在映射器中忽略它们。

  2. 您将拥有显式映射,这些映射将忽略不需要的内容,并使用限定符选择适当的方法。约预选赛更多信息此处的文档中

  3. 使用最新版本1.2.0(在回答的时候1.2.0.RC1,并使用新的@Context参数。有一个看地图,用周期从mapstruct实例库,解决了循环映射问题。你不必使用Object,也可以使用您的特定类型。

注意:该1.2.0版本不提供“开箱即用”的循环映射解决方案,它需要由用户明确地完成。