我什么时候应该在RMI中实现java.io.Serializable?

我刚刚启动Java RMI并且在使用java.io.Serializable时遇到一些问题,所以任何人都可以给我一个必须实现java.io.Serializable的RMI示例.

谢谢!!!

更新:
我做了一个简单的例子,但是,我认为由于输出不正确,仍然存在问题.
人机界面

包服务器;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public interface PersonInterface extends Remote  
{
    public void setName(String name) throws RemoteException;
    public String getPerson() throws RemoteException;
    public void setAddress(Address address) throws RemoteException;
}

人员实施

package server;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.Naming;
import java.rmi.Remote;

class Person extends UnicastRemoteObject implements PersonInterface
{
    private String name;
    private int age;
    private Address address;


    Person() throws RemoteException {super();}
    Person(String name,int age, Address address) throws RemoteException {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public void setName(String name) throws RemoteException {
        this.name = name;
    }
    public void setAddress(Address address) throws RemoteException{
        this.address = address;
    }

    public String getPerson() throws RemoteException {
        return "Person : " + name + " age : " + age + " address : " + address;
    }
}

地址类

package server;
import java.io.Serializable;

public class Address implements Serializable
{
    private static final long serialVersionUID = 227L;
    private String addre1;
    private String addre2;

    public Address() {}
    public Address(String addre1,String addre2){
        this.addre1 = addre1;
        this.addre2 = addre2;   
    }
}

服务器

package server;
import java.rmi.Naming;

class Server 
{
    public static void main(String[] args) 
    {
        try{
        //create an instance of the RemoteDatabaseServer
            Person person = new Person();
            //rmi://[host][:port]/object
            String namePerson = "rmi://localhost:9999/person";

            //bind this instance to localhost port999 with name database
            Naming.bind(namePerson, person);
            System.out.println("Server is running...");
        }catch(Exception ex){
            System.out.println("Server Exception...");
            ex.printStackTrace();
        }
    }
}

客户

package client;
import java.rmi.RMISecurityManager;
import java.rmi.Naming;
import server.PersonInterface;
import server.Address;


class Client
{
    public static void main(String[] args) 
    {
        try{
            System.setSecurityManager(new RMISecurityManager());
            String namePerson = "rmi://localhost:9999/person";
            PersonInterface person = 
                (PersonInterface)Naming.lookup(namePerson);

            person.setName("myName");
            System.out.println(person.getPerson());
            person.setName("myNewName");
            Address address = new Address("123","123");
            person.setAddress(address);
            System.out.println(person.getPerson());
        }catch(Exception ex){
            System.out.println("Client failure...");
            ex.printStackTrace();
        }
    }
}

我得到的输出是

D:\java -Djava.security.policy=d:\Client\policy\client.policy client.Client
Person : myName age : 0 address : server.Address@1d6776d
Person : myNewName age : 0 address : server.Address@10a2d64

地址打印不正确
PS:
正如您从Client类导入中看到的那样

import server.PersonInterface;
import server.Address;

我将PersonInterface.class和Address.class复制到客户端以编译客户端.

最后:
如此愚蠢!!!
将以下代码添加到Address.java

public String toString(){
    return addre1+ " " + addre2;
}

好的,问题解决了!!

上一篇:java – RMI服务器异常


下一篇:docker常用命令