仅在 spring 启动应用程序中出现 UnknownFieldException

UnknownFieldException only in spring boot app

我使用 XStream 解组 Data.xml 文件。 当我 运行 它与我的 main() 函数一起工作时它完美地工作,但是当我尝试通过我的启动应用程序 运行 它时,它给我 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

当我调用此方法时,创建 Main 对象失败并出现此异常,但如果我从 "public static void main..." 调用同一行,它会完美运行

@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;

}

在 Main 内部使用此行解组 Data.xml

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

知道为什么会出现这种奇怪的行为吗?

谢谢

Update

我注意到 XStreamAlias 注释被忽略了,所以我添加了 xstream.autodetectAnnotations(真); 但现在我得到了这个例外:

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

我正在使用这一行来解组 xml:

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

正在添加

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

已修复。