Java RMI Object/DTO 传递ClassCastException

Java RMI Object/DTO transfer ClassCastException

当我想通过 rmi 服务取回 DTO 时遇到 ClassCastException。 我的东西:

模块 A:

    @Bean
    public RmiProxyFactoryBean rmiProxyFactoryBean() {
        RmiProxyFactoryBean proxyFactory = new RmiProxyFactoryBean();
        proxyFactory.setServiceUrl("rmi://localhost:1099/FileApi");
        proxyFactory.setServiceInterface(FileApi.class);
        return proxyFactory;
    }

Module_B:

    @Bean
    public RmiServiceExporter rmiServiceExporter(final FileApi implementation) {
        Class<FileApi> serviceInterface = FileApi.class;
        RmiServiceExporter exporter = new RmiServiceExporter();
        exporter.setServiceInterface(serviceInterface);
        exporter.setService(implementation);
        exporter.setServiceName(serviceInterface.getSimpleName());
        exporter.setRegistryPort(1099);
        return exporter;
    }
    @Data
    public class FileDto implements Serializable {
        private static final long serialVersionUID = 8226436902318340588L;

        private final String streamId;
        private final String name;
        private final boolean isDirectory;

    }
    public interface FileApi {
        FileDto uploadFile(final UploadDto uploadDto);
    }

问题:当我调用 FileDto uploadFile(final UploadDto uploadDto); 并且我想使用结果 dto 然后我得到这个:

    java.lang.ClassCastException: class hu.teszt.FileDto cannot be cast to class hu.teszt.FileDto (hu.teszt.FileDto is in unnamed module of loader 'app'; hu.teszt.FileDto is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @6ea914f)
        at com.sun.proxy.$Proxy144.uploadFile(Unknown Source) ~[na:na]

我试图在 FileDto.class 中实现 java.rmi.Remote,但我遇到了同样的异常。我做错了什么?

这与 devtools 包有关。见 following issue on GitHub:

It will happen indeed with any library that deserialize content. Caching libraries, in particular, are affected.

您可以从您的 pom 中删除 devtools 包或不实现 Serializable 接口,看看是否能解决问题。