ClassCastException in StaxEventItemReader

微笑、不失礼 提交于 2019-12-11 08:18:27

问题


I have a problem with ClassCaseException. I created XStreamItemEventReader in Spring batch to read xml and write result to database. Readers i reading xml which should be parsed into that class:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmployeeDTO {

    private Long id;
    private String firstName;
    private String surname;
    private String email;
    private Integer age;
}

My item reader for xml is create in that class :

public final class XMLReader<T> extends StaxEventItemReader<T> implements ClosableItemReader<T> {
    public XMLReader(Map<String, Object> parameters) {

        setResource(new FileSystemResource((File) parameters.get(RESOURCE)));
        setFragmentRootElementName((String) parameters.get(ROOT_TAG));

        Map<String, String> aliases = (Map<String, String>) parameters.get(ALIASES_MAP);
        XStreamMarshaller marshaller = new XStreamMarshaller();
        marshaller.setAliases(aliases);
        setUnmarshaller(marshaller);
        setStrict(false);
        open(new ExecutionContext());
    }

And a am reading in all employees by XMLReader in that class

public class EmployeeReader implements ItemReader<EmployeeDTO> {


    private final JobImportReaderFactory jobImportReaderFactory;
    private ClosableItemReader<EmployeeDTO> itemReader;

    public EmployeeReader(JobImportReaderFactory jobImportReaderFactory, JobFileRepository jobFileRepository) {
        this.jobImportReaderFactory = jobImportReaderFactory;
        this.jobFileRepository = jobFileRepository;
    }

    @BeforeStep
    public void setup() throws IOException {
        // this factory gives me XMLReader instance
        itemReader = jobImportReaderFactory.getItemReader(jobEnum, readerParameters, EmployeeDTO.class);

    }

    @Override
    public EmployeeDTO read() throws Exception {
        log.debug("read()");
        return itemReader.read();
    }
}

My exception is look like that:

java.lang.ClassCastException: class com.kk.tutorial.domain.dtos.EmployeeDTO cannot be cast to class com.kk.tutorial.domain.dtos.EmployeeDTO (com.kk.tutorial.domain.dtos.EmployeeDTO is in unnamed module of loader 'app'; com.kk.tutorial.domain.dtos.EmployeeDTO is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @24eb3739)

Ii try to implement Serializable in EmployeeDTO and it does not work. I know that this is the problem of ClassLoaders but i dont know how to deal with ClassLoader and how to case value from itemReader.read() to EmployeeDTO by another ClassLoader. Do you have any idea how to repair that casting ?


回答1:


Removal spring-boot-devtools from pom.xml repaire an issue



来源:https://stackoverflow.com/questions/57061393/classcastexception-in-staxeventitemreader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!