Xstream使用过程出现: .java.lang.ClassCastException

文章介绍了在SpringBoot项目中使用Xstream处理XML文件时遇到的两个主要问题及解决方案。第一个问题是由于非默认classloader导致的ClassCastException,解决方法是手动设置Xstream的classloader。第二个问题是升级Xstream版本后出现的ForbiddenClassException,通过调用allowTypes方法授予所需类权限得以解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Xstream使用过程出现:
1.java.lang.ClassCastException
最近在一个springboot项目中使用Xstream,将xml文件读取通过注解的方式转换成java实体类,报错 java.lang.ClassCastException,找了很久的原因,那我在这里感谢这位博主

错误:

j

ava.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);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值