Xstream ClassCastException ForbiddenClassException 同一個類轉換異常

Xstream

1.java.lang.ClassCastException

最近在一個springboot項目中使用Xstream,將xml文件讀取通過註解的方式轉換成java實體類,報錯 java.lang.ClassCastException,找了很久的原因,在這裏感謝這位博主轉載自

錯誤:

java.lang.ClassCastException: com.jt.bean.xml.ComconfigParse cannot be cast to com.jt.bean.xml.ComconfigParse
	at com.jt.component.ConfigParseAware.parseXml(ConfigParseAware.java:48)
	at com.jt.component.ConfigParseAware.setApplicationContext(ConfigParseAware.java:31)

原因:
因爲springboot項目中不是使用的默認classloader。

解決方法:
手動重設xtream的classloader。

    XStream xt = new XStream();
    //YourObject爲你的項目任意實體類名
    xt.setClassLoader(YourObject.class.getClassLoader());

 

2.com.thoughtworks.xstream.security.ForbiddenClassException

在pom.xml中使用高版本的Xstream後:

        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.11.1</version>
        </dependency>

會報以下錯誤:

com.thoughtworks.xstream.security.ForbiddenClassException: com.jt.bean.xml.ComconfigParse
	at com.thoughtworks.xstream.security.NoTypePermission.allows(NoTypePermission.java:26)

需給予指定權限,如下:

//ComconfigParse,Comconfig,Parse爲xml對應的實體類
xstream.allowTypes(new Class[]{ComconfigParse.class, Comconfig.class, Parse.class});

XML完整讀取代碼如下:

package com.jt.component;

import com.jt.bean.xml.Comconfig;
import com.jt.bean.xml.ComconfigParse;
import com.jt.bean.xml.Parse;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;

/**
 * @Author:
 * @Description:
 * @CreateDate: 2019/11/6
 */
@Component
@Slf4j
public class ConfigParseAware implements ApplicationContextAware {

    public static ComconfigParse configParse;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        parseXml();
    }

    /**
     * 加載config xml
     */
    private void parseXml() {
        log.info("加載configParse.xml");
        try {
            File file = ResourceUtils.getFile("classpath:comconfigParse.xml");
            FileInputStream ins = new FileInputStream(file);
            XStream xstream = new XStream(new DomDriver("utf-8"));
            xstream.autodetectAnnotations(true);
            XStream.setupDefaultSecurity(xstream);
            xstream.allowTypes(new Class[]{ComconfigParse.class, Comconfig.class, Parse.class});
            xstream.setClassLoader(ComconfigParse.class.getClassLoader());
            xstream.processAnnotations(ComconfigParse.class);
            configParse = (ComconfigParse) xstream.fromXML(ins);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            log.error("加載configParse.xml異常", e);
        }
    }
}

版權聲明:本文編號1內容爲博主LineRain原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

轉載文鏈接:https://blog.csdn.net/u014724586/article/details/86478144

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章