Itai Soudry
Itai Soudry

Reputation: 350

UnknownFieldException only in spring boot app

I use XStream to unmarshal Data.xml files. When I run it with my main() function it works perfectly, but when I try to run it through my boot app, it gives me UnknownFieldException.

Caused by: com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field workshop.tokenizer.data.Attributes.attribute ---- Debugging information ---- message : No such field workshop.tokenizer.data.Attributes.attribute field : attribute class : workshop.tokenizer.data.Attributes required-type : workshop.tokenizer.data.Attributes converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter class[1] : workshop.tokenizer.data.DataObj class[2] : java.util.ArrayList converter-type[1] : com.thoughtworks.xstream.converters.collections.CollectionConverter class[3] : workshop.tokenizer.wrappers.DataObjects version : 1.4.9

when i'm calling this method, creating Main object fail with this exception, but if I call the same line from "public static void main..." it works perfectly

@RequestMapping(method = RequestMethod.POST, value = "/test")
public ResponseEntity<SFRecord> execute(@RequestBody RequestDto json)
        throws ClassNotFoundException, SQLException, FileNotFoundException {
    Request request = json.convert();
    Main main = new Main();
    RequestObj reqObj = main.execute(request.getSentence());
    ResponseEntity<SFRecord> result = restTemplate.exchange(parserUrl, HttpMethod.POST,
            new HttpEntity<RequestObj>(reqObj), SFRecord.class);
    return result;

}

inside Main is use this line to unmarshal Data.xml

FileReader reader = new FileReader("Files/Data.xml");
    objects = (DataObjects) xstream.fromXML(reader);

any ideas why the strange behavior?

Thanks

Update

I noticed that the XStreamAlias annotations are being ignored, so I added xstream.autodetectAnnotations(true); but now I get this exception:

java.lang.ClassCastException: wrappers.DataObjects cannot be cast to wrappers.DataObjects

I'm using this line to unmarshal the xml:

FileReader reader = new FileReader("Files/Data.xml");    
DataObjects objects = (DataObjects) xstream.fromXML(reader);

Upvotes: 0

Views: 746

Answers (1)

Itai Soudry
Itai Soudry

Reputation: 350

Adding

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

fixed it.

Upvotes: 1

Related Questions