Reputation: 159
I am trying to converting a List of obect of type FilerDetailsExcelFileBean(package A of project A) into byte array.After successfully conversion in project A. I passed byte array to web service and then again byte array converted to List of obect of type FilerDetailsExcelFileBean (package B of project B) in project B.In order to get back list of object from byte array I created same file FilerDetailsExcelFileBean in both projects because package B cannot import package A files due to design pattern.
code to convert ListOfObject into byte array:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(List);
byte[] bytes = bos.toByteArray();
code to convert byte array into ListOfObject:
ByteArrayInputStream bis = new ByteArrayInputStream(filerMarkingFile.getRequestFile());
ObjectInputStream ois = new ObjectInputStream(bis);
List<FilerDetailsExcelFileBean> f = (List<FilerDetailsExcelFileBean>) ois.readObject();
errors is: FilerDetailsExcelFileBean classNotFound exception
Note: list of type FilerDetailsExcelFileBean file is in different project-A (code to convert ListOfObject into byte array:) and FilerDetailsExcelFileBean file is in different project-B (code to convert byte array into ListOfObject:).and FilerDetailsExcelFileBean import is not possible into project-B due to some reasons.
I know the issue(issue is that when we converted arrayList to byte array,different persistent file is used and when we converted it back to list ob object, different persistent file is used) but I need to solve this problem.What is the best solution of this problem
Upvotes: 0
Views: 37
Reputation: 206996
That will not work, a packageA.FilerDetailsExcelFileBean
is not the same class as packageB.FilerDetailsExcelFileBean
, so you cannot serialize the first one and then deserialize it as the second one. The fact that the class names and even the layout of the class is the same, is not enough. If you want to do this with Java serialization, then the classes must be exactly the same - they must also be in the same package.
Instead of using Java serialization, use a different mechanism - for example, convert the object to JSON or XML and parse it on the other side.
Upvotes: 2