带名称空间解组的JAXB(使用REST服务中的Jersey) [英] JAXB with namespace unmarshalling (using Jersey from REST service)

查看:73
本文介绍了带名称空间解组的JAXB(使用REST服务中的Jersey)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从Convio的公共api解组一个简单的xml文档。我没有使用以下代码获得任何编译器错误,但它也不会产生结果。值为null。如果我从xml文档中删除模式和命名空间项并从POJO中删除命名空间属性,那么它将运行得很好。我能错过什么才能使用xsd文档/命名空间?

I'm trying to unmarshal a simple xml document from a public api from Convio. I'm not getting any compiler errors with the following code, but it won't produce a result either. The values are null. If I remove the schema and namespace items from the xml document and remove the namespace attribute from the POJO then it will run just fine. What am I missing to be able to work with the xsd document / namespace?

我试图解析的XML示例

XML example that I'm trying to parse

<?xml version='1.0' encoding='UTF-8'?>
<getSingleSignOnTokenResponse xsi:schemaLocation="http://convio.com/crm/v1.0 http://service.convio.net/xmlschema/crm.public.v1.xsd" xmlns="http://convio.com/crm/v1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <token>abcdefghijklmnopqrstuvwxyz</token>
  <cons_id>0123456789</cons_id>
</getSingleSignOnTokenResponse>

带有注释的POJO:

@XmlRootElement(name = "getSingleSignOnTokenResponse", namespace = "http://convio.com/crm/v1.0")
public class SingleSignOnResponseBean
{
  @XmlElement(name = "token")
  public String token;
  @XmlElement(name = "cons_id")
  public int consId;
}

现在,我正在使用泽西岛做实际的工作,但是因为我无法使用Jersey解组,我使用上面XML结果的机器上的静态xml文件手动设置了一个unmarshaller:

Now, I'm using Jersey to do the actual work, but since I couldn't get it to unmarshal using Jersey, I set up an unmarshaller by hand using a static xml file on my machine of the XML result above:

    JAXBContext jc = JAXBContext.newInstance(new Class[] {org.orgname.utility.convio.sso.api.SingleSignOnResponseBean.class});
    Unmarshaller u = jc.createUnmarshaller();
    SingleSignOnResponseBean bean2 = (SingleSignOnResponseBean) u.unmarshal(new File("C:/token.xml"));
    System.out.println(bean2.token);

这可能非常简单,我只是没有看到为什么它不起作用架构和名称空间元素已定义。我已经看到了一些关于设置某种SAX过滤器去除命名空间的评论,但由于我是通过直接来自泽西的REST调用进来的,所以我不相信我有机会这样做。任何想法?

This is probably very simple and I'm just not seeing it on why it won't work if the schema and namespace elements are defined. I've seen some other comments about setting up some sort of SAX filter to strip out the namespace, but since I'm coming in via a REST call from jersey directly I don't believe I have the opportunity to do that. Any ideas?

推荐答案

命名空间不是由绑定类的字段继承。您还需要在字段上定义命名空间:

The namespace is not "inherited" by the fields on the bound class. You need to define the namespace on the fields as well:

@XmlRootElement(name = "getSingleSignOnTokenResponse", namespace = "http://convio.com/crm/v1.0")
public class SingleSignOnResponseBean
{
  @XmlElement(name = "token", namespace = "http://convio.com/crm/v1.0")
  public String token;
  @XmlElement(name = "cons_id", namespace = "http://convio.com/crm/v1.0")
  public int consId;
}

如果省略它们,则字段将返回默认命名空间(即没有命名空间)。

If you omit them, then the fields go back to the "default" namespace (i.e. no namespace).

这有点令人恼火,但就是这样。

It's mildly irritating, but that's the way it is.

这篇关于带名称空间解组的JAXB(使用REST服务中的Jersey)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆