java - 如何设置 ModelMapper 来嵌套到不同的嵌套?

标签 java spring modelmapper

Flight 对象具有如下属性:

@JsonProperty("OriginLocation")
private Location originLocation;

位置对象如下所示:

@JsonProperty("lat")
private double latitude;
@JsonProperty("lon")
private double longitude;

我正在映射到 FlightDto,其中属性如下所示(注意:FlightDto 将 5 个左右属性从 Flight 移动到 LocationDto - 除了纬度/经度之外,这些属性都会自动映射):

private LocationDto origin;

LocationDto 看起来像这样:

private CoordinatesDto coordinates;

坐标Dto看起来像这样:

private double latitude;
private double longitude;

所以...除了纬度/经度之外,所有属性都正确映射。映射应该是:

FlightDto.getOrigin().setCoordinates(new CoordinatesDto(Flight.getOriginLocation().getLatitude(),
                             Flight.getOriginLocation().getLongitude());

FlightDto.getOrigin() 中还有其他字段以某种方式自动正确映射。

我尝试这样做:

    modelMapper.addMappings(new PropertyMap<Flight, FlightDto>() {
        @Override
        protected void configure() {
            map().getOrigin().setCoordinates(new CoordinatesDto(source.getOriginLocation().getLatitude(),
                                                                    source.getOriginLocation().getLongitude()));
//              map().getDestination().setCoordinates(new CoordinatesDto(source.getDestinationLocation().getLatitude(),
//                                                                       source.getDestinationLocation().getLongitude()));

    }
});

但这实际上在启动时崩溃了:

Caused by: java.lang.NullPointerException: null at org.xxx.yyy.flights.models.Location$ByteBuddy$NcO8GjcN.getLongitude(Unknown Source) ~[classes/:na] at org.xxx.yyy.config.ModelMapperConfig$2.configure(ModelMapperConfig.java:42) ~[classes/:na] at org.modelmapper.PropertyMap.configure(PropertyMap.java:389) ~[modelmapper-2.3.5.jar:na]

我不明白为什么 getLatitude() 有效,但 getLongitude() 不起作用...但可以说我注释掉了 getLongitude() 并硬编码了 1 在那里...看起来像 setter 仅在启动时调用一次,而不是针对每个记录调用一次。

我还看到了一些使用 TypeMap 的示例,但我看不到如何从 1 个嵌套级别映射到 2 个嵌套级别,因为 LocationDto 已正确创建和填充,但 LocationDto.Cooperatives 却没有。

最佳答案

我认为您这里有两个问题,请参阅下面的代码和注释:

// When you have different field names you should tell it to ModelMapper like this
mm.createTypeMap(Flight.class, FlightDto.class).addMapping(Flight::getOriginLocation,
    FlightDto::setOrigin);

// And when you do not have 'clear' association or mapping is not in the `same 
// level` mapping can not be guessed so you need to tell it explicitly like 
// (this is something near to what you tried already)
mm.addMappings(new PropertyMap<Location, LocationDto>() {
    @Override
    protected void configure() {
        map().getCoordinates().setLatitude(source.getLatitude());
        map().getCoordinates().setLongitude(source.getLongitude());
    }
});

告诉这个(这两个)之后 - 假设我正确理解你的 model/dto 结构 - ModelMapper 能够进行映射。

关于java - 如何设置 ModelMapper 来嵌套到不同的嵌套?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59082019/

相关文章:

java - 如果在 catch 子句中抛出异常,finally 仍然运行

java - IS Camel 从 jetty 获取 http 请求

java - 获取删除触发器的当前用户 - Spring MyBatis - Liquibase

java - ModelMapper:如何处理枚举的空值

java - ModelMapper 在将实体转换为 DTO 时产生异常

java - 创建具有多个 View 、许多组件和内存问题的大型 Web 应用程序的最佳实践

java - Spring Security openId 支持和用户解除认证

在 Spring Java Web 应用程序中实现 RIOT CMS 时出现 java.lang.ClassNotFoundException

java - Spring Boot 自动配置顺序

java - ModelMapper:将集合映射到其他结构的集合