weixin_44116104 2022-04-02 10:40 采纳率: 100%
浏览 97
已结题

flex.messaging无法解析自己创建的Amf3格式消息,如何解决?(语言-java)

在实验创建Amf3格式消息时,能够正常serialize消息,却不能deserialize消息。想求问原因
public class Demo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
    Student st=new Student();
    byte[] _bs =Amf3_3.serialize(st);
    Student st2= (Student) Amf3_3.deserialize(_bs);
}

public class Amf3_3 {
  public static byte[] serialize(Object data) throws IOException {
    MessageBody body = new MessageBody();
    body.setData(data);

    ActionMessage message = new ActionMessage();
    message.addBody(body);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    AmfMessageSerializer serializer = new AmfMessageSerializer();
    serializer.initialize(SerializationContext.getSerializationContext(), out, null);
    serializer.writeMessage(message);

    return out.toByteArray();
  }

  public static Object deserialize(byte[] amf) throws ClassNotFoundException, IOException {
    ByteArrayInputStream in = new ByteArrayInputStream(amf);
    AmfMessageDeserializer deserializer = new AmfMessageDeserializer();
    deserializer.initialize(SerializationContext.getSerializationContext(), in, null);
    ActionMessage message=new ActionMessage();
    deserializer.readMessage(message, new ActionContext());
    return  message.getBody(0);
  }
 }

public class Student implements Serializable{
  private static final long serialVersionUID = 1L;
  public String account;
  public String tClass;

  public Student() {
    this.account = "111";
    this.tClass = "222";
  }

  @Override
  public String toString() {
    return "Student{" +
            "account='" + account + '\'' +
            ", tClass='" + tClass + '\'' +
            '}';
   }
 }
运行结果及报错内容

Exception in thread "main" flex.messaging.io.SerializationException: Creation validation for class 'Student' failed.
at flex.messaging.util.ClassUtil.validateCreation(ClassUtil.java:354)
at flex.messaging.util.ClassUtil.createDefaultInstance(ClassUtil.java:115)
at flex.messaging.io.AbstractProxy.createInstanceFromClassName(AbstractProxy.java:95)
at flex.messaging.io.AbstractProxy.createInstance(AbstractProxy.java:115)
at flex.messaging.io.amf.AbstractAmfInput.createObjectInstance(AbstractAmfInput.java:169)
at flex.messaging.io.amf.Amf3Input.readScriptObject(Amf3Input.java:748)
at flex.messaging.io.amf.Amf3Input.readObjectValue(Amf3Input.java:156)
at flex.messaging.io.amf.Amf3Input.readObject(Amf3Input.java:134)
at flex.messaging.io.amf.Amf0Input.readObjectValue(Amf0Input.java:124)
at flex.messaging.io.amf.Amf0Input.readObject(Amf0Input.java:95)
at flex.messaging.io.amf.AmfMessageDeserializer.readObject(AmfMessageDeserializer.java:199)
at flex.messaging.io.amf.AmfMessageDeserializer.readBody(AmfMessageDeserializer.java:173)
at flex.messaging.io.amf.AmfMessageDeserializer.readMessage(AmfMessageDeserializer.java:93)
at Amf3_3.deserialize(Amf3_3.java:28)
at Demo.main(Demo.java:28)
断开与目标 VM 的连接,地址:'127.0.0.1:1113', transport: 'socket'

进程已结束,退出代码 1

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

4条回答 默认 最新

  • 关注
    
    
    import java.io.IOException;
    
    public class Demo {
        public static void main(String[] args) throws IOException, ClassNotFoundException {
            //创建
            Student st = Student.builder().account("aa").tClass("bb").build();
            //序列化
            byte[] _bs = Amf3_3.serialize(st);
            //反序列化
            System.out.println(Amf3_3.deserialize(_bs));
        }
    }
    
    
    
    
    
    import flex.messaging.io.SerializationContext;
    import flex.messaging.io.amf.*;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    
    public class Amf3_3 {
        public static byte[] serialize(Object data) throws IOException {
            MessageBody body = new MessageBody();
            body.setData(data);
            ActionMessage message = new ActionMessage();
            message.addBody(body);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            AmfMessageSerializer serializer = new AmfMessageSerializer();
            serializer.initialize(SerializationContext.getSerializationContext(), out, null);
            serializer.writeMessage(message);
            return out.toByteArray();
        }
    
        public static Object deserialize(byte[] amf) throws ClassNotFoundException, IOException {
            ByteArrayInputStream in = new ByteArrayInputStream(amf);
            AmfMessageDeserializer deserializer = new AmfMessageDeserializer();
            deserializer.initialize(SerializationContext.getSerializationContext(), in, null);
            ActionMessage message = new ActionMessage();
            deserializer.readMessage(message,new ActionContext());
            MessageBody body = message.getBody(0);
            return body.getData();
        }
    }
    
    
    
    
    import lombok.AllArgsConstructor;
    import lombok.Builder;
    import lombok.NoArgsConstructor;
    
    import java.io.Serializable;
    
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public class Student implements Serializable {
    
        private static final long serialVersionUID = -6511660272442746949L;
        public String account;
        public String tClass;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月11日
  • 已采纳回答 4月3日
  • 赞助了问题酬金5元 4月2日
  • 修改了问题 4月2日
  • 展开全部

悬赏问题

  • ¥15 Mac虚拟机安装Windows,Windows中用idea跑项目,报错process finished with exit code -1073741819(0xc0000005) 如何解决?
  • ¥15 关于python爬虫返回js数据结构的处理
  • ¥15 ubuntu所用stm32标准库开发烧录没反映
  • ¥15 运维工程师(在职人员回答)
  • ¥15 python 爬虫问题
  • ¥15 Kmeans的趋势分析有会的吗
  • ¥20 R语言单细胞测序 对seurat对象进行标准化时出现问题
  • ¥15 Anaconda navigator updateor一直更新
  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。