Search code examples
javarmiclasscastexception

Java RMI Object/DTO transfer ClassCastException


I have got a ClassCastException when i want to get back a DTO through rmi service. My stuff:

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

The problem: When i call FileDto uploadFile(final UploadDto uploadDto); and i want to use the result dto then i get this:

    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]

I tried to implement java.rmi.Remote in FileDto.class, but i get the same exception. What did i wrong?


Solution

  • This is related to the devtools package. See the following issue on GitHub:

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

    You can either remove the devtools package from your pom or don't implement the Serializable interface, see if that solves the issue.