Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Avro发送对象/POJO_Java_Object_Pojo_Avro - Fatal编程技术网

Java 使用Avro发送对象/POJO

Java 使用Avro发送对象/POJO,java,object,pojo,avro,Java,Object,Pojo,Avro,我有一个服务器-客户机模型,客户机发送一条hello消息,我希望服务器发回一个带有特定字段的对象。我正在使用Avro进行通信 Avro模式: "hello": {"request":[{"name": "name", "type": "string"}], "response": "bytes" } ByteBuffer hello(CharSequence name) throws AvroRemoteException, IOException; 服务器端: publ

我有一个服务器-客户机模型,客户机发送一条hello消息,我希望服务器发回一个带有特定字段的对象。我正在使用Avro进行通信

Avro模式:

"hello": {"request":[{"name": "name", "type": "string"}],
          "response": "bytes" } 
ByteBuffer hello(CharSequence name) throws AvroRemoteException, IOException;
服务器端:

public ByteBuffer hello(CharSequence name) throws IOException {
        Log.info("[Server]: Method 'hello' was invoked...");
        Log.info("[Server]: Name: "+ name.toString());

        Obj obj = new Obj();
        obj.setDate(new Date());
        obj.setName("bla hello");

        return ByteBuffer.wrap(ByteTransformation.getBytesFromObject(obj));
        //return ByteBuffer.wrap(new Utf8("UO").getBytes());
    }
客户端:

    ByteBuffer response = client.sayHello("Jo");
            System.out.println(response.remaining());
            Obj obj;
            try {
                obj =(Obj)ByteTransformation.getObjectFromBytes(response.array());
                System.out.println(obj.name);

            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
//========================================================
public ByteBuffer sayHello(String name) throws IOException {
        Log.info("[Client] : Method 'sayHello' was invoked...");
        if (client==null)
            return null;
        Mail proxy = (Mail) SpecificRequestor.getClient(Mail.class, client);
        return proxy.hello(new Utf8(name));
    }
序列化/反序列化

public class ByteTransformation {

    public static byte [] getBytesFromObject(Object object) throws IOException {
        byte [] result;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = null;

        try {
            out = new ObjectOutputStream(bos);   
            out.writeObject(object);
            result = bos.toByteArray();
        } finally {
            try {
                if(out!=null)
                    out.close();
            } catch(IOException ex) {
                //DO NOTHING
            }
            try {
                bos.close();
            } catch(IOException ex) {
                //DO NOTHING
            }            
        }

        return result;
    }

    public static Object getObjectFromBytes(byte [] bytes) throws IOException, ClassNotFoundException {
        Object result = null;
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInput in = null;
        try {
            in = new ObjectInputStream(bis);
            result = in.readObject();
        } finally {
            try {
                if(in!=null)
                    in.close();
            } catch(IOException ex) {
                //DO NOTHING
            }
            try {
                bis.close();
            } catch(IOException ex) {
                //DO NOTHING
            }
        }
        return result;
    }
问题:java.io.NotSerializableException:org.thesis.avrorpcserver.MailImpl$Obj

到目前为止,我只尝试发送像字符串这样的简单的工作人员,它起了作用,当我发送一个对象时,我得到了这个对象。。。为什么?