MapperFacade使用

引入依赖

<dependency>
   <groupId>ma.glasnost.orika</groupId>
   <artifactId>orika-core</artifactId>
   <version>1.5.4</version>
</dependency>

配置类

import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OrikaConfig {
    @Bean
    public MapperFactory mapperFactory() {
        return new DefaultMapperFactory.Builder().build();
    }

    @Bean
    public MapperFacade mapperFacade() {
        return mapperFactory().getMapperFacade();
    }
}

使用举例

@Autowired
private MapperFacade mapperFacade;

DTO dto = mapperFacade.map(domain, DTO.class);
List<DTO> dtoList = mapperFacade.mapAsList(domain, DTO.class);

报错排查

报错:

An exception occured while compiling the following method:

public void mapAtoB(java.lang.Object a, java.lang.Object b, ma.glasnost.orika.MappingContext mappingContext) {

super.mapAtoB(a, b, mappingContext);

// sourceType: ZonedDateTime
java.time.ZonedDateTime source = ((java.time.ZonedDateTime)a);
// destinationType: ZonedDateTime
java.time.ZonedDateTime destination = ((java.time.ZonedDateTime)b);

……

for ma.glasnost.orika.generated.Orika_ZonedDateTime_ZonedDateTime_Mapper196016043448600$1

javassist.CannotCompileException: [source error] getChronology() not found in java.time.ZonedDateTime

……

Resolved [ma.glasnost.orika.MappingException: While attempting the following mapping:
sourceType = java.time.ZonedDateTime
destinationType = java.time.ZonedDateTime
Error occurred: ma.glasnost.orika.impl.generator.CompilerStrategy$SourceCodeGenerationException: Error compiling ma.glasnost.orika.generated.Orika_ZonedDateTime_ZonedDateTime_Mapper196016043448600$1]

原因:ZoneDateTime类型映射异常,具体原因未知,升级版本得以解决(orika-core原本使用的1.5.0,改为1.5.4之后无此异常)、

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值