使用 IDResolver 时解组 JAXB 集合失败,因为 IDResolver 对象作为目标类型

发布于 2024-12-05 23:36:08 字数 1726 浏览 0 评论 0原文

我在尝试从其余 Web 服务 (cxf) 中解组 json 时遇到此问题。 我正在使用 JAXB 和 EclipseLink。

实体的映射如下:

@Entity
@Table(name = "service_pkg_service", schema = "MD")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ServicePkgService extends DatabaseModel implements java.io.Serializable {

    @Transient
    @XmlIDREF
    private Set<ChannelPkgService> channelPkgServices = new HashSet<ChannelPkgService>();
}

@Entity
@Table(name = "channel_pkg_service", schema = "MD")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class ChannelPkgService extends DatabaseModel  implements java.io.Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "CHANNEL_PKG_ID")
    @XmlID
    @XmlAttribute
    private String id;

}

我有一个扩展 IDResolver 的类,因此我可以根据其 ID 生成一个实体。

public class EntityIDResolver extends IDResolver{

@Override
public void bind(String id, Object obj) throws SAXException {
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Callable<?> resolve(final String id, Class targetType) throws SAXException {

    }
}

我在解组 json 时遇到问题,如下所示 "channelPkgService": [1,2,3],targetType 的类是 java.lang.Object

我读了这个 https://github.com/javaee/jaxb-v2/issues/546 ,并创建了一个包装器来处理此问题。

public class ChannelPkgServiceWrapper extends HashSet<ChannelPkgService>{
}

由于我有很多这样的情况,我不想创建很多包装器,有没有更通用的方法来处理这个问题?

忘记使用的版本:

  • cxf.version:2.3.6
  • eclipselink:2.3.0
  • jaxb-impl-2.1.13.jar (包含 Lister.class 的 jar 正在执行获取正确类型的实际工作。)

I have this problem trying to unmarshall json from rest webservice (cxf).
I'm using JAXB and EclipseLink.

The entity is mapped like that:

@Entity
@Table(name = "service_pkg_service", schema = "MD")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ServicePkgService extends DatabaseModel implements java.io.Serializable {

    @Transient
    @XmlIDREF
    private Set<ChannelPkgService> channelPkgServices = new HashSet<ChannelPkgService>();
}

@Entity
@Table(name = "channel_pkg_service", schema = "MD")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class ChannelPkgService extends DatabaseModel  implements java.io.Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "CHANNEL_PKG_ID")
    @XmlID
    @XmlAttribute
    private String id;

}

I have a class extending IDResolver, so I can generate an entity based on its ID.

public class EntityIDResolver extends IDResolver{

@Override
public void bind(String id, Object obj) throws SAXException {
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Callable<?> resolve(final String id, Class targetType) throws SAXException {

    }
}

I have problem unmarshalling json like this "channelPkgService": [1,2,3], the class of targetType is java.lang.Object

I read this https://github.com/javaee/jaxb-v2/issues/546 , and created a wrapper to handle this.

public class ChannelPkgServiceWrapper extends HashSet<ChannelPkgService>{
}

Sinse I have a lot of these cases and I don't want to create a lot of wrappers, is there a more generic way to handle that?

Forget the used versions:

  • cxf.version:2.3.6
  • eclipselink:2.3.0
  • jaxb-impl-2.1.13.jar
    (jar containing Lister.class that is doing the actual work for getting the correct type.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛徒 2024-12-12 23:36:08

JAXB 使用 @XmlIDREF 来映射文档内引用。 ID 引用的每个对象还必须嵌套在 XML 或 JSON 文档中的某个位置:

如果您正在寻找要将对象编组为其 ID,那么您将需要使用 XmlAdapter。查看我对类似问题的回答:

另请注意,JAXB 是一个规范(JSR-222) 和 EclipseLink 包含 MOXy 实施(我是 MOXy 技术负责人)。这意味着您可以从依赖项中消除 jaxb-impl-2.1.13.jar:

@XmlIDREF is used by JAXB to map intra-document references. Each object referenced by ID must also appear nested somewhere in the XML or JSON document:

If you are looking to marshal an object as its ID, then you will want to use an XmlAdapter. Check out my answer to a similar question:

Also note that JAXB is a specification (JSR-222), and EclipseLink contains the MOXy implementation (I'm the MOXy tech lead). This means you could eliminate jaxb-impl-2.1.13.jar from your dependencies:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文