在使用orika 做bean工具的时候,如果beanA的某属性类型为Integer ,要转换为beanB的同名属性的类型是一个enum,这个时候,就会出现如下报错: Error occurred: ma.glasnost.orika.Ma
在使用orika 做bean工具的时候,如果beanA的某属性类型为Integer ,要转换为beanB的同名属性的类型是一个enum,这个时候,就会出现如下报错:
Error occurred: ma.glasnost.orika.MappingException: Encountered mapping of enum to object (or vise-versa); sourceType=Integer, destinationType=GenderEnum
Caused by: ma.glasnost.orika.MappingException: Encountered mapping of enum to object (or vise-versa); sourceType=Integer, destinationType=GenderEnum
at ma.glasnost.orika.impl.generator.specification.UnmappableEnum.generateMappinGCode(UnmappableEnum.java:41)
at ma.glasnost.orika.impl.generator.SourceCodeContext.mapFields(SourceCodeContext.java:759)
我们找到这个UnmappableEnum类,可以看到此处的抛错代码
public class UnmappableEnum extends AbstractSpecification {
public boolean appliesTo(FieldMap fieldMap) {
return fieldMap.getBType().isEnum() && !fieldMap.getAType().isEnum() && !fieldMap.getAType().isString();
}
public String generateMappingCode(FieldMap fieldMap, VariableRef source, VariableRef destination, SourceCodeContext code) {
throw new MappingException("Encountered mapping of enum to object (or vise-versa); sourceType="+
source.type() + ", destinationType=" + destination.type());
}
}
public DefaultCodeGenerationStrategy() {
this.specifications = new CopyOnWriteArrayList<Specification>(
Arrays.asList(
new ConvertArrayOrCollectionToArray(),
new ConvertArrayOrCollectionToCollection(),
new Convert(),
new CopyByReference(),
new ApplyReGISteredMapper(),
new EnumToEnum(),
new StringToEnum(),
new UnmappableEnum(),
new ArrayOrCollectionToArray(),
new ArrayOrCollectionToCollection(),
new MapToMap(),
new MapToArray(),
new MapToCollection(),
new ArrayOrCollectionToMap(),
new StringToStringConvertible(),
new AnyTypeToString(),
new MultioccurrenceElementToObject(),
new ObjectToMultiOccurrenceElement(),
new PrimitiveAndObject(),
new ObjectToObject()));
this.aggregateSpecifications = new CopyOnWriteArrayList<AggregateSpecification>(
Arrays.asList(new MultiOccurrenceToMultiOccurrence()));
}
我们来试试,在不改动orika的情况下,怎么通过配置,让其支持Integer转Enum。
环境:group: ‘com.gitlab.haynes’, name: ‘orika-spring-boot-starter’, version: ‘1.26.0’
UserA
@Data
@With
@AllArgsConstructor
@NoArgsConstructor
public class UserA {
private String name;
private Integer age;
private Integer gender;
}
@Data
@With
@AllArgsConstructor
@NoArgsConstructor
public class UserB {
private String name;
private Integer age;
private GenderEnum gender;
}
@AllArgsConstructor
public enum GenderEnum {
MALE(1,"男"),
FEMALE(2,"女");
@Getter
@Setter
private Integer value;
@Setter
@Getter
private String label;
}
@Test
public void t1(){
UserA userA = new UserA().withName("test").withAge(10).withGender(1);
UserB userB = mapperFacade.map(userA, UserB.class);
System.out.println(userB);
}
@AllArgsConstructor
public enum GenderEnum {
MALE(1,"男"),
FEMALE(2,"女");
@Getter
@Setter
private Integer value;
@Setter
@Getter
private String label;
public static GenderEnum of(Integer value){
if(Objects.isNull(value)){
return null;
}
for (GenderEnum item: GenderEnum.values()){
if(Objects.equals(item.getValue(),value)){
return item;
}
}
return null;
}
}
@Component
public class UserMapping implements OrikaMapperFactoryConfigurer {
@Override public void configure(MapperFactory mapperFactory) {
mapperFactory.claSSMap(UserA.class,UserB.class).exclude("gender").byDefault().customize(
new CustomMapper<UserA, UserB>() {
@Override public void mapAtoB(UserA userA, UserB userB, MappingContext context) {
userB.setGender(GenderEnum.of(userA.getGender()));
}
}).register();
}
}
UserB(name=test, age=10, gender=MALE)
@Component
public class GenderConverter extends CustomConverter<Integer,GenderEnum> {
@Override public GenderEnum convert(Integer source, Type<? extends GenderEnum> destinationType,
MappingContext mappingContext) {
return GenderEnum.of(source);
}
}
@RequiredArgsConstructor
@Component
public class GlobalMapperFactoryConfigurer implements OrikaMapperFactoryConfigurer {
private final List<Converter<?,?>> converters;
@Override public void configure(MapperFactory orikaMapperFactory) {
ConverterFactory converterFactory = orikaMapperFactory.getConverterFactory();
if(!CollectionUtils.isEmpty(converters)){
converters.forEach(converterFactory::registerConverter);
}
}
}
UserB(name=test, age=10, gender=MALE)
--结束END--
本文标题: orika 中解决UnmappableEnum抛错的错误
本文链接: https://lsjlt.com/news/99256.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0