Xstream ClassCastException ForbiddenClassException 同一个类转换异常

4 篇文章 0 订阅

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

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值