当前位置: 首页 > 知识库问答 >
问题:

Orika:java.lang.ClassCastException:java.lang.Object不能强制转换为Mapper.Name

丁景山
2023-03-14

我在这里的一节中做示例orika映射器文档,映射数组和列表的html" target="_blank">元素。

    public class Name {
        private String first;
        private String last;
        private String fullName;
        // getters/setters

        public Name() {

        }

        public Name(String first, String last, String fullName) {
            this.first = first;
            this.last = last;
            this.fullName = fullName;
        }

        public String getFirst() {
            return first;
        }

        public void setFirst(String first) {
            this.first = first;
        }

        public String getLast() {
            return last;
        }

        public void setLast(String last) {
            this.last = last;
        }

        public String getFullName() {
            return fullName;
        }

        public void setFullName(String fullName) {
            this.fullName = fullName;
        }
    }
import java.util.List;

public class Person {
 private List<Name> names;
 // getters/setters


 public Person() {
 }

public List<Name> getNames() {
    return names;
}

 public void setNames(List<Name> names) {
    this.names = names;
 }
}

PersonDto类定义-

import java.util.List;
import java.util.Map;

public class PersonDto {
    private Map<String, Name> personalNames;
    private String[] firstNames;
    private List<String> lastNames;
    // getters/setters omitted

    public PersonDto() {
    }

    public Map<String, Name> getPersonalNames() {
        return personalNames;
    }

    public void setPersonalNames(Map<String, Name> personalNames) {
        this.personalNames = personalNames;
    }

    public String[] getFirstNames() {
        return firstNames;
    }

    public void setFirstNames(String[] firstNames) {
        this.firstNames = firstNames;
    }

    public List<String> getLastNames() {
        return lastNames;
    }

    public void setLastNames(List<String> lastNames) {
        this.lastNames = lastNames;
    }
}


    Map<String,String> fieldMap = new HashMap<>();
    fieldMap.put("names{fullName}", "personalNames{key}");
    fieldMap.put("names{}", "personalNames{value}");
    MapperFactory mapperFactory = new DefaultMapperFactory.Builder().mapNulls(false).dumpStateOnException(false).build();
    ClassMapBuilder<Person, PersonDto> classBuilder = mapperFactory.classMap(Person.class, PersonDto.class);
    fieldMap.forEach((k,v) -> classBuilder.field(k,v));
    classBuilder.register();
    BoundMapperFacade<Person, PersonDto> delegate = mapperFactory.getMapperFacade(Person.class, PersonDto.class);



    Person person = new Person();
    Name n1 = new Name("raj", "kumar", "raj kumar");
    Name n2 = new Name("senthil", "kumar", "senthil kumar");
    person.setNames(Arrays.asList(n1, n2));
    PersonDto pDto = mapper.map(person);
    System.out.println(pDto);

当我运行这段代码时,我得到以下错误-

java.lang.Object不能强制转换为mapper.name java.lang.ClassCastException:java.lang.Object不能强制转换为ma.glasnost.orika.generated.orika_persondto_person_mapper12572858142441$0.mapatob(Orika_Persondto_person_mapper12572858142441$0.java)在在ma.glasnost.orika.impl.defaultboundMapperfacade.map(DefaultboundMapperfacade.java:94),在mapper.persontopersondTomapper.map(PersontopersondTomapper.java:29),在mapper.persontopersondTomapper.map(PersontopersondTomapper.java:14),在mapper.persontopersondTomapper.test1(PersontopersondTomapper.java:29),在ect.delegatingMethodAccessorImpl.Invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.method.invoke(method.java:498),在org.testng.internal.MethodInvocationHelper.invokeMethod(methodInvocationHelper.java:133),在org.testng.internal.testInvoker.invokeMethod(testInvoker.java:584),在org.testng.internal.testInvoker.java:172),在org.testng.internal.methodInvoker.java:46,在4)在org.testng.internal.testinvoker.invoketestmethods(testinvoker.java:145)在org.testng.internal.testmethodworker.java:146)在org.testng.internal.testmethodworker.run(testmethodworker.java:128)在java.util.arraylist.foreach(arraylist.java:1249)在org.testng.testrunner.privaterun(testrunner.java:770)在unner.java:402)在org.testng.suiterunner.runSequentury(suiterunner.java:396),在org.testng.suiterunner.privaterun(SuiteRu)nner.java:355)位于org.testng.suiterunner.run(suiterunner.java:304)

我在这里的github上托管了这段代码。

我的代码有什么问题?如何解决此问题?

共有1个答案

谢嘉
2023-03-14

出现这个问题是因为Java泛型在运行时被删除了。map.entry 编译时)变为map.entry (运行时)。

您可以通过如下调整ClassMapBuilder来克服这个问题:

Map<String, String> fieldMap = new HashMap<>();
fieldMap.put("names{fullName}", "personalNames{key}");
fieldMap.put("names{}", "personalNames{value}");
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().mapNulls(false).dumpStateOnException(false).build();
// Swap Person, PersonDto generic arguments.
ClassMapBuilder<PersonDto, Person> classBuilder = mapperFactory.classMap(PersonDto.class, Person.class);
fieldMap.forEach((k, v) -> classBuilder.field(v, k));
classBuilder.register();
// rest of the code is the same
 类似资料: