ModelMapper:将列表对象(带有连接字符串)映射到另一个对象列表

时间:2019-03-02 22:31:47

标签: java modelmapper

我已经在整个互联网上寻找解决方案,但是找不到适合我的解决方案。

我想映射以下课程

类名:


    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({
            "Make",
            "Model"
    })
    public class CarType {

        @JsonProperty("Make")
        private String make;
        @JsonProperty("Model")
        private String model;
        @JsonProperty("VIN")
        private String vin;
    (... getter/setter ...)

...到...


    @ApiModel(description = "FleetContractDto")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class FleetContractDto implements Serializable {
        (... other attributes ...)
        @ApiModelProperty(value = "Type of the car (generated)",
                example = "BMW i3")
        private String carType = null;
        (... getter/setter ...)

CarType类的容器


    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({
            "Count",
            "Results"
    })
    public class CarTypeContainer {

        @JsonProperty("Count")
        private Integer count;
        @JsonProperty("Results")
        private List results = null;

    (... getter/setter ...)

FleetContractDto的容器:


    @ApiModel(description = "FleetContainer")
    public class FleetContainer implements Serializable {
        @SerializedName("data")
        private List data = new ArrayList();
    (... getter / setter ...)

我尝试了很多解决方案,例如TypeToken


    List fleetContracts = mappingRepo.getModelMapper().map(carTypes.getResults(), new TypeToken>() {}.getType());

但是问题是,我已经有了一个包含对象(以及其中的值)的列表,并且我不想有一个新列表。

所以我为ModelMapper编写了两个映射类:

容器容器:


    public class CarTypeContainerToFleetContainer extends PropertyMap {

        private static final Converter, List> carTypeConverter = ctx -> {
            return ctx.getSource().stream().map(carType -> {
                MappingContext subContext = ctx.create(carType, FleetContractDto.class);
                return ctx.getMappingEngine().map(subContext);
            }).collect(Collectors.toList());
        };

        @Override
        protected void configure() {
            using(carTypeConverter).map(source.getResults()).setData(null);
        }
    }

将CarType转换为FleetContractDto:


    public class CarTypeToFleetContractDto extends PropertyMap {

        private static final Converter carTypeConverter = ctx -> {
            return ctx.getSource().getMake() + " " + ctx.getSource().getModel();
        };

        @Override
        protected void configure() {
            using(carTypeConverter).map().setCarType(null);
        }
    }

还添加了一些内容,这些内容将在Spring之前找到:

this.modelMapper.addMappings(new CarTypeContainerToFleetContainer());
this.modelMapper.addMappings(new CarTypeToFleetContractDto());

问题是,如果我试图从CarTypeContainerToFleetContainer-Mapper调用CarTypeToFleetContractDto-Mapper,我将得到以下错误:

ModelMapper mapping errors:\r\n\r\n1) Converter (...).dto.mapper.fleet.CarTypeToFleetContractDto$$Lambda$44/1246567323@43de4d77 failed to convert java.lang.Object to java.lang.String.\r\nCaused by: java.lang.NullPointerException\r\n\tat (...).dto.mapper.fleet.CarTypeToFleetContractDto.lambda$static$0(CarTypeToFleetContractDto.java:11)\r\n\tat org.modelmapper.internal.MappingEngineImpl.convert(MappingEngineImpl.java:302)\r\n\tat org.modelmapper.internal.MappingEngineImpl.setDestinationValue(MappingEngineImpl.java:239)\r\n\tat org.modelmapper.internal.MappingEngineImpl.propertyMap(MappingEngineImpl.java:187)\r\n\tat org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:151)\r\n\tat org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:105)

我没有更多的想法可以继续尝试。我唯一想做的就是将CarTypes列表中的值映射到FleetContractDtos已经存在的列表中。

如果有人可以帮助我,那就太好了。

0 个答案:

没有答案