java - 使用 Jersey 解码 xml 标签

标签 java rest jersey

我有这个 xml:

<?xml version="1.0" encoding="utf-8"?>
 <ProductItem xmlns="http://providers.natinst.com/pdi-rest/1.0/meta/" urn="urn:product-item:910796-76" url="http://immix-test2.natinst.com/pdi-rest/1.0/en-US/product-item/910796-76.xml">
<partNumber>910796-76</partNumber>
<inventoryItemId>560427</inventoryItemId>
<nicInventoryItemId>765430</nicInventoryItemId>
<name />
<description>LABVIEW CORE 2 SELF-PACED ONLINE TRAINING (6 MONTHS ACCESS)</description>
<isCustomerFacing>true</isCustomerFacing>
<itemType>CE</itemType>
<partType>Training Program</partType>
<bookingsClassName />
<bookingsClassCode />
<lifecyclePhase>Released</lifecyclePhase>
<salesClass />
<firstOrderableDate />
<locale>en-US</locale>
<ngpmProductHierarchy />
<productRevisions url="http://immix-test2.natinst.com/pdi-rest/1.0/en-US/product-item/910796-76/productRevisions.xml" />
<serviceOptionsForProduct url="http://immix-test2.natinst.com/pdi-rest/1.0/en-US/product-item/910796-76/serviceOptionsForProduct.xml" />
<serviceOptionsByService url="http://immix-test2.natinst.com/pdi-rest/1.0/en-US/product-item/910796-76/serviceOptionsByService.xml" />
<productFeatures url="http://immix-test2.natinst.com/pdi-rest/1.0/en-US/product-item/910796-76/productFeatures.xml" />
 </ProductItem>

我正在尝试获取描述标签。这是我的 Java 代码。

package com.ni.apps.elearningrest.client;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


import javax.xml.bind.annotation.XmlElementWrapper;

@XmlRootElement(name = "ProductItem")
public class DescriptionDTO {

    private String description;
    private String uri;


    @XmlElement(name = "description")
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }               
}

但我不断收到“意外元素(uri:”http://providers.natinst.com/pdi-rest/1.0/meta/,local:“ProductItem”)。预期元素是 <{}ProductItem>”错误。我该怎么做才能解决这个问题?

最佳答案

虽然您可以在 @XmlRootEleemnt@XmlElement 注释上指定 namespace 属性,但我建议利用 @XmlSchema 注释来指定取而代之的是默认命名空间。

com/ni/apps/elearningrest/client/package-info.java

@XmlSchema( 
    namespace = "http://providers.natinst.com/pdi-rest/1.0/meta/", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package com.ni.apps.elearningrest.client;

了解更多信息

关于java - 使用 Jersey 解码 xml 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10688369/

相关文章:

java - 允许数字范围的正则表达式,或 null

java - 通过数据源为 H2 设置事务隔离级别

swift - 在 Swift 中将视频上传到 YouTube

ios - 如何从 Rest API 发送推送通知

java - Jersey : response with List<DBObject>

java - 一种将字符解析为 Unicode 的函数在 Eclipse 中有效,但在 JDeveloper 中无效?

java - 从排序列表中删除重复项

java - Spring Rest json 输出

jersey - 如何从 ContainerResponseFilter 内部获取源地址/ip

java - 用Java/jersey/grizzly编写的微服务的线程配置