JAXB with namespace unmarshalling (using Jersey from REST service)(具有命名空间解组的 JAXB(使用来自 REST 服务的 Jersey))
问题描述
我正在尝试从 Convio 的公共 api 中解组一个简单的 xml 文档.以下代码我没有收到任何编译器错误,但它也不会产生结果.值为空.如果我从 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:
And the POJO with annotations:
@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 来完成实际工作,但由于我无法使用 Jersey 对其进行解组,因此我在我的机器上使用静态 xml 文件手动设置了一个解组器,上面的 XML 结果:
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:具有命名空间解组的 JAXB(使用来自 REST 服务的 Jersey)


- Eclipse 插件更新错误日志在哪里? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01