关于JAXB xml转对象失败

这篇博客探讨了在使用JAXB进行XML到对象转换时遇到的问题。具体表现为尝试将`ParamMethod`对象转换为`InterfaceContainer`,导致`ClassCastException`。博主详细展示了`MethodContainer`、`ParamMethod`类的结构,并分析了可能的错误原因。

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

@XmlRootElement(name="interfaceContainer")
public class InterfaceContainer {
    //接口名
    private String interfaceName;
    //接口方法
    private List<MethodContainer> methods;
    //接口描述
    private String comment;
    //接口创建人
    private String creator;
    //接口创建时间
    private Date createDate;
    //接口修改人
    private String modifier;
    //修改时间
    private Date updateDate;
    //生成时间
    private Date generatedDate;
    //状态
    private int state;
    //get  set
    @XmlElement
    public String getInterfaceName() {
        return interfaceName;
    }
    
    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }
    @XmlElementWrapper(name = "methods")
    @XmlElement(name = "method")
    public List<MethodContainer> getMethods() {
        return methods;
    }
    public void setMethods(List<MethodContainer> methods) {
        this.methods = methods;
    }
    @XmlElement
    public String getComment() {
        return comment;
    }
    public void setComment(String comment) {
        this.comment = comment;
    }
    @XmlElement
    public String getCreator() {
        return creator;
    }
    public void setCreator(String creator) {
        this.creator = creator;
    }
    @XmlElement
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
    @XmlElement
    public String getModifier() {
        return modifier;
    }
    public void setModifier(String modifier) {
        this.modifier = modifier;
    }
    @XmlElement
    public Date getUpdateDate() {
        return updateDate;
    }
    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }
    @XmlElement
    
    public Date getGeneratedDate() {
        return generatedDate;
    }
    public void setGeneratedDate(Date generatedDate) {
        this.generatedDate = generatedDate;
    }
    @XmlElement
    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

   



这个是最外层的对象,




public class MethodContainer {
    //方法名称
    private String methodName;
    //方法参数
    private List<ParamMethod> param;
    //方法返回值
    private ParamMethod returned;
    //方法修饰词
    private String decorated;
    //方法体
    private String methodBody;
    //get  set方法
    @XmlElement
    public String getMethodName() {
        return methodName;
    }
    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }
    @XmlElementWrapper(name = "params")
    @XmlElement(name = "param")
    public List<ParamMethod> getParam() {
        return param;
    }
    public void setParam(List<ParamMethod> param) {
        this.param = param;
    }
    @XmlElement
    public String getDecorated() {
        return decorated;
    }
    public void setDecorated(String decorated) {
        this.decorated = decorated;
    }
    @XmlElement
    public ParamMethod getReturned() {
        return returned;
    }
    public void setReturned(ParamMethod returned) {
        this.returned = returned;
    }
    @XmlElement
    public String getMethodBody() {
        return methodBody;
    }
    public void setMethodBody(String methodBody) {
        this.methodBody = methodBody;
    }
    
   



这个是中间对象


@XmlRootElement(name="interfaceContainer")
public class ParamMethod {
    //参数类型
    
    private String paramClazz;
    //参数名
    
    private String paramName;
    //get  set
    @XmlElement
    public String getParamClazz() {
        return paramClazz;
    }
    public void setParamClazz(String paramClazz) {
        this.paramClazz = paramClazz;
    }
    @XmlElement
    public String getParamName() {
        return paramName;
    }
    public void setParamName(String paramName) {
        this.paramName = paramName;
    }
   


这个是底层对象  ,


java.lang.ClassCastException: com.entity.ParamMethod cannot be cast to com.entity.InterfaceContainer


这个是异常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值