MapperFacade使用

引入依赖

<dependency>
   <groupId>ma.glasnost.orikagroupId>
   <artifactId>orika-coreartifactId>
   <version>1.5.4version>
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之后无此异常)、

你可能感兴趣的:(JAVA,MapperFacade)