You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JAXB反序列化XML至列表:Error类属性值为null问题求助

问题分析与解决方案

你的问题核心是Error类的注解使用错误:XML中<type>和<message>是<error>的子元素,但你在代码里用了@XmlAttribute(用于绑定XML属性,比如<id version="1">里的version),这导致JAXB无法将子元素的值映射到属性上,所以type和message都是null。

修正步骤:

  1. 修改Error类的注解
    将@XmlAttribute替换为@XmlElement,因为这两个是XML子元素,不是属性:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "error", propOrder = {
            "type",
            "message"
    })
    public class Error implements Serializable {
    
        private final static long serialVersionUID = -1L;
        @XmlElement(required = true)
        protected String type;
        @XmlElement(required = true)
        protected String message;
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
    
  2. 优化Content类的列表绑定(可选但推荐)
    为确保JAXB正确识别多个<error>元素,建议在error列表字段上添加@XmlElement(name = "error")注解,同时初始化列表避免空指针:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "content", propOrder = {
            "status",
            "securitiesFinancingTrade",
            "schemaVersion",
            "error"
    })
    public class Content implements Serializable {
    
        private final static long serialVersionUID = -1L;
        protected ContentStatusEnumType status;
        protected SecuritiesFinancingTrade securitiesFinancingTrade;
        protected String schemaVersion;
        @XmlElement(name = "error", required = true)
        protected List<Error> error = new ArrayList<>();
    
        public ContentStatusEnumType getStatus() {
            return status;
        }
    
        public void setStatus(ContentStatusEnumType status) {
            this.status = status;
        }
    
        public SecuritiesFinancingTrade getSecuritiesFinancingTrade() {
            return securitiesFinancingTrade;
        }
    
        public void setSecuritiesFinancingTrade(SecuritiesFinancingTrade securitiesFinancingTrade) {
            this.securitiesFinancingTrade = securitiesFinancingTrade;
        }
    
        public String getSchemaVersion() {
            return schemaVersion;
        }
    
        public void setSchemaVersion(String schemaVersion) {
            this.schemaVersion = schemaVersion;
        }
    
        public List<Error> getError() {
            if (error == null) {
                error = new ArrayList<>();
            }
            return error;
        }
    
        public void setError(List<Error> error) {
            this.error = error;
        }
    }
    

验证说明

修改后重新反序列化XML,JAXB会正确将<error>下的<type>和<message>子元素的值映射到Error对象的对应属性中,同时多个<error>元素会被正确收集到列表里。

内容的提问来源于stack exchange,提问作者Jakub Znamenáček

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 11:15:35