Nodecom.thoughtworks.xstream.mapper.CannotResolveClassException when using xstream under a teapot

I use XStream under Kettle to deserialize XML to a Java object and always give me an exception: nodecom.thoughtworks.xstream.mapper.CannotResolveClassException

Then I tried my code separately from Kettle and as a simple Java application. and it works great. For instance:

 public static void main(String[] args) { person p = new person("JJ", "MM"); XStream xstream = new XStream(new DomDriver()); xstream.alias("personname", person.class); String xml = xstream.toXML(p); person pp = (person) xstream.fromXML(xml); System.out.println(pp.toString()); } public class person { private String firstname; private String lastname; public person(String first, String last) { this.firstname = first; this.lastname = last; } public String getFirstname() { return this.firstname; } public String getLastname() { return this.lastname; } public void setFirstname(String name) { this.firstname = name; } public void setLastname(String name) { this.lastname = name; } } 

And this code is working fine. However, when I move this code to the Kettle plugin, it does not work at the stage of reading metadata from the XML file.

+6
source share
2 answers

I was able to fix this problem. I had to install the class loader for the XStream instance, which I use to de-serialize the xml string.

Therefore, before calling xstream.fromXml(xml) do the following:

  xstream.setClassLoader(person.class.getClassLoader()); 

This will resolve the xstream.mapper.CannotResolveClassException exception. This is really weird. Hope this helps.

+9
source
 *xstream.alias("personname", person.class);* 

change it to include the class name, it will work

 xstream.alias("person", person.class); 
+3
source

Source: https://habr.com/ru/post/909692/


All Articles