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

JAX-WS服务返回自定义对象为空,需配置什么注解?

问题原因及修复方案

核心问题

你的ApplicationConstant自定义类不符合JAXB序列化规则,JAX-WS默认依赖JAXB完成Java对象与XML的转换,你缺失了两个必要条件:

  • 必须提供公共无参构造方法:JAXB序列化对象时需要先通过无参构造实例化对象
  • 类属性的读取方法必须符合JavaBean命名规范:必须以get/is开头,JAXB只会识别符合规范的getter方法读取属性值,你现有的group()、subGroup()这类方法名不符合要求,序列化时读取不到任何属性值,最终返回空的XML节点

可选优化配置

建议给自定义类添加@XmlRootElement注解,显式声明该类可被JAXB序列化,避免不同JAX-WS实现的兼容问题。

修复后的ApplicationConstant代码示例

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class ApplicationConstant {

    // 新增公共无参构造
    public ApplicationConstant() {
    }

    public ApplicationConstant(String group, String subGroup, String id, String code, String text, String desc ) {
        this.group = group;
        this.subGroup = subGroup;
        this.id = id;
        this.text = text;
        this.code = code;
        this.desc = desc;
    }

    // 所有getter改为符合JavaBean规范的命名
    public String getGroup() { return group; }
    public String getSubGroup() { return subGroup; }
    public String getConstantIdentifier() { return id; }
    public String getConstantText() { return text; }
    public String getConstantCode() { return code; }
    public String getConstantDesc() { return desc; }


    private String group;
    private String subGroup;
    private String id;
    private String text;
    private String code;
    private String desc;    

}

额外校验点

如果修改后仍存在问题,可检查以下配置:

  • 确认@WebResult注解的targetNamespace和自定义类的命名空间配置一致,避免命名空间不匹配导致属性不展示
  • 可先在本地单独测试JAXB序列化ApplicationConstant对象的结果,确认序列化出来的XML包含所有属性后再发布到服务中验证

内容的提问来源于stack exchange,提问作者Greycon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 23:39:03