我有ClassCaseException问题。我在Spring批处理中创建了XStreamItemEventReader,以读取xml并将结果写入数据库。读者我读了应该解析为该类的xml:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmployeeDTO {

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


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

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读取所有员工

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的未命名模块中)

我尝试在EmployeeDTO中实现Serializable,但是它不起作用。我知道这是ClassLoader的问题,但我不知道如何处理ClassLoader以及如何通过另一个ClassLoader将value从itemReader.read()传递给EmployeeDTO。您知道如何修理该铸件吗?

最佳答案

从pom.xml删除spring-boot-devtools修复问题

07-24 15:13