Well, I got the same error, but caching was not the reason. I actually used caching, but commenting on caching did not help.
Based on the prompts here and there, I just introduced the extra serialization / derivation of my object. This is definitely the best way (performance issue), but it works.
So, just for the rest, I changed my code:
@Cacheable("tests") public MyDTO loadData(String testID) { // add file extension to match XML file return (MyDTO) this.xmlMarshaller.loadXML(String.format("%s/%s.xml", xmlPath, testID)); }
in
@Cacheable("tests") public MyDTO loadData(String testID) { // add file extension to match XML file Object dtoObject = this.xmlMarshaller.loadXML(String.format("%s/%s.xml", xmlPath, testID)); byte[] data = serializeDTO(dtoObject); MyDTO dto = deserializeDTO(data); return dto; } private MyDTO deserializeDTO(byte[] data) { MyDTO dto = null; try { ByteArrayInputStream fileIn = new ByteArrayInputStream(data); ObjectInputStream in = new ConfigurableObjectInputStream(fileIn, Thread.currentThread().getContextClassLoader()); dto = (MyDTO) in.readObject(); in.close(); fileIn.close(); } catch (Exception e) { String msg = "Deserialization of marshalled XML failed!"; LOG.error(msg, e); throw new RuntimeException(msg, e); } return dto; } private byte[] serializeDTO(Object dtoObject) { byte[] result = null; try { ByteArrayOutputStream data = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(data); out.writeObject(dtoObject); out.close(); result = data.toByteArray(); data.close(); } catch (IOException e) { String msg = "Serialization of marshalled XML failed!"; LOG.error(msg, e); throw new RuntimeException(msg, e); } return result; }
Note: this is not some complicated solution, but just a hint of using the ConfigurableObjectInputStream class.
source share