本文介绍了StaxEventItemReader 中的 ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 ClassCaseException 问题.我在 Spring 批处理中创建了 XStreamItemEventReader 来读取 xml 并将结果写入数据库.我正在阅读 xml 的读者应该将其解析为该类:

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;
}

我的 xml 项目阅读器是在该类中创建的:

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());
    }

并且正在通过 XMLReader 在该类中读取所有员工

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();
    }
}

我的例外是这样的:

java.lang.ClassCastException:类 com.kk.tutorial.domain.dtos.EmployeeDTO 不能转换为类 com.kk.tutorial.domain.dtos.EmployeeDTO(com.kk.tutorial.domain.dtos.EmployeeDTO 是在加载器app"的未命名模块中;com.kk.tutorial.domain.dtos.EmployeeDTO 在加载器 org.springframework.boot.devtools.restart.classloader.RestartClassLoader @24eb3739 的未命名模块中)

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)

我尝试在 EmployeeDTO 中实现 Serializable 但它不起作用.我知道这是 ClassLoader 的问题,但我不知道如何处理 ClassLoader 以及如何通过另一个 ClassLoader 将 itemReader.read() 中的值转换为 EmployeeDTO.你知道如何修复那个铸件吗?

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 ?

推荐答案

从 pom.xml 中删除 spring-boot-devtools 依赖将修复问题.

Removing the spring-boot-devtools dependency from pom.xml will repair the issue.

更新

开发工具使用单独的类加载器,这会导致给定的问题.

The devtools are using a separate Classloader which leads to the given problem.

这篇关于StaxEventItemReader 中的 ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 14:16