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)
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01