您好,欢迎来到欧得旅游网。
搜索
您的当前位置:首页Xstream使用过程出现: .java.lang.ClassCastException

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

来源:欧得旅游网

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);
        }
    }
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- ovod.cn 版权所有 湘ICP备2023023988号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务