【问题标题】:Jackson XML annotations: Extract single string value from XML element with attributesJackson XML 注释:从具有属性的 XML 元素中提取单个字符串值
【发布时间】:2017-09-24 07:46:50
【问题描述】:

我正在使用 Jackson XML 注释将 XML 文档从外部 API 转换为 POJO。 XML 中的一个元素给我带来了一些麻烦。大多数元素没有属性,只有一个文本值,例如:

<title>Title Here</title>

我在使用一个元素时遇到了一些问题,它有一个属性,如下所示:

<urgency id="UrgCaution">Caution</urgency>

我只想提取文本值“Caution”并将其存储在一个字符串中。我最初在我的 Java 类中尝试过这种方式:

public class Item {
    @JacksonXmlProperty(localName = "urgency")
    private String urgency;
}

但这会导致以下错误:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct 
instance of Item: no String-argument constructor/factory method to deserialize from 
String value ('Security')
at [Source: java.io.ByteArrayInputStream@328ff654; line: 40, column: 21] 

“安全性”是称为“标签”的 xml 属性的文本,该属性随后出现在文档中。

我尝试进行以下更改,这消除了异常,但导致我在 POJO 中获得一个空值来表示紧急情况。

public class Item {
    @JacksonXmlProperty(localName = "urgency")
    @JacksonXmlText
    private String urgency;
}

我觉得我在这里遗漏了一些明显的东西。使用 Jackson 将此元素的文本转换为 POJO 上的字符串字段的最佳方法是什么?

这是我正在使用的完整类和 XML 文档。

XML:

<rss version="2.0">
    <channel>
        <title>Title</title>
        <description>Description</description>
        <copyright>Copyright info</copyright>
        <ttl>5</ttl>
        <pubDate>Thu, 27 Apr 2017 17:21:40 GMT</pubDate>
        <item>
            <title>Title of event in U.S.</title>
            <description>Description here</description>
            <fulldescription>Full description here</fulldescription>
            <link>http://www.website.com</link>
            <pubDate>Thu, 27 Apr 2017 17:13:48 GMT</pubDate>
            <locations>
                <location>
                    <name>New York, NY</name>
                    <latitude>40.714502</latitude>
                    <longitude>-74.006998</longitude>
                    <code>NYC</code>
                    <city>New York</city>
                    <state>New York</state>
                    <country>United States</country>
                    <region>North America</region>
                </location>
            </locations>
            <source>AP</source>
            <urgency id="UrgAttention">Attention</urgency>
            <categories>
                <category id="CatTransp">Transportation</category>
            </categories>
            <destinations>
                <destination code="NYC" id="US" lat="40.714502" longitude="-74.006998">New York, New York</destination>
            </destinations>
            <designations />
            <related />
            <tags>Railway disruptions</tags>
            <guid>3d64388fc639475dc9aeaabf81ee87bd</guid>
        </item>
    </channel>
</rss>

现在上课。为简洁起见,省略了 getter 和 setter。还有一个 Location、Destination 和 Category 类,但除非必要,否则我不会包括它们。

根类:

@JacksonXmlRootElement(localName = "rss")
public class FeedRoot {
    @JacksonXmlProperty(localName = "channel")
    private FeedChannel channel;
}

频道类:

public class FeedChannel {
    @JacksonXmlElementWrapper(localName = "item", useWrapping = false)
    @JacksonXmlProperty(localName = "item")
    private List<Item> items;
}

Item 类(Json 属性用于将结果编组为 JSON,但删除它们不会改变结果):

public class Item {
    @JacksonXmlProperty(localName = "title")
    private String title;

    @JacksonXmlProperty(localName = "description")
    private String description;

    @JacksonXmlProperty(localName = "fulldescription")
    @JsonProperty("full_description")
    private String fullDescription;

    @JacksonXmlProperty(localName = "link")
    private String link;

    @JacksonXmlProperty(localName = "pubDate")
    @JsonProperty("publish_date")
    private String pubDate;

    @JacksonXmlElementWrapper(localName = "locations")
    @JacksonXmlProperty(localName = "location")
    private List<Location> locations;

    @JacksonXmlProperty(localName = "source")
    private String source;

    @JacksonXmlProperty(localName = "urgency")
    private String urgency;

    @JacksonXmlElementWrapper(localName = "categories")
    @JacksonXmlProperty(localName = "category")
    private List<Category> categories;

    @JacksonXmlElementWrapper(localName = "destinations")
    @JacksonXmlProperty(localName = "destination")
    private List<Destination> destinations;

    @JacksonXmlProperty(localName = "related")
    private String related;

    @JacksonXmlProperty(localName = "tags")
    private String tags;

    @JacksonXmlProperty(localName = "guid")
    private String guid;

    //getters and setters below here, no annotations
}

尝试解析时出错(省略命名空间):

"Could not read document: Can not construct instance of 
com.namespacestuff.Item: no String-argument constructor/factory 
method to deserialize from String value ('Railway disruptions')\n at 
[Source: java.io.ByteArrayInputStream@45563ea1; line: 42, column: 32] 
(through reference chain: com.namespacestuff.FeedRoot[\"channel\"]-
>com.namespacestuff.FeedChannel[\"item\"]->java.util.ArrayList[5]); 
nested exception is 
com.fasterxml.jackson.databind.JsonMappingException: Can not 
construct instance of com.namespacestuff.Item: no String-argument 
constructor/factory method to deserialize from String value ('Railway 
disruptions')\n at [Source: java.io.ByteArrayInputStream@45563ea1; 
line: 42, column: 32] (through reference chain: 
com.namespacestuff.FeedRoot[\"channel\"]-
>com.namespacestuff.FeedChannel[\"item\"]->java.util.ArrayList[5])"

【问题讨论】:

  • 我认为 @JacksonXmlText 不正确。也许您必须提供 POJO 中的所有属性。
  • 你的意思是剩下的xml元素吗?如果是这样,我已经在这样做了。

标签: java xml jackson pojo


【解决方案1】:

拥有这个

<urgency id="UrgCaution">Caution</urgency>

您必须创建一个 POJO,如下所示:

@JacksonXmlRootElement(localName = "urgency")
public class Urgency {
    @JacksonXmlProperty(isAttribute=true)
    String id;
    @JacksonXmlText
    String text;
}

【讨论】:

    【解决方案2】:

    更新

    由于在我回答后更新了 OP,我添加了一些注意事项。

    即使使用提供的代码,如果我尝试仅反序列化 Item 元素,一切都很好。

    问题出现在所有 rss 元素都涉及时。

    查看问题在 FeedChannel 定义上的 XML。

    public class FeedChannel {
        @JacksonXmlElementWrapper(localName = "item", useWrapper = false)
        @JacksonXmlProperty(localName = "item")
        private List<Item> items;
    }
    

    我尝试将这个删除 useWrapperlocalName 更改为 items,然后我相应地更改了 XML。

    <items><item>...</item></items>
    

    一切正常。

    正如我在下面所写的,urgency 属性不需要 @JacksonXmlText 注释。

    它看起来像 Jackson 上的一个错误,因为 useWrapper 不仅会更改您当前列表的行为。

    urgency 属性属性id 将属性转换为数组。 如果您删除该属性,一切都很好。

    到目前为止,我认为这是杰克逊图书馆的问题,您应该向他们报告此案。


    原答案

    你错过了发布所有需要的代码,因为你的错误不是我认为的地方。

    我做了一个有效的例子:

    这是我的 POJO:

    @JacksonXmlRootElement(localName = "item")
    public class Item {
        @JacksonXmlProperty(localName = "field1")
        private String field1;
    
        @JacksonXmlProperty(localName = "field2")
        private String field2;
    
        @JacksonXmlProperty(localName = "urgency")
        private String urgency;
    
        @JacksonXmlProperty(localName = "tags")
        private String tags;
    
        public String getField1() {
            return field1;
        }
    
        public void setField1(String field1) {
            this.field1 = field1;
        }
    
        public String getField2() {
            return field2;
        }
    
        public void setField2(String field2) {
            this.field2 = field2;
        }
    
        public String getUrgency() {
            return urgency;
        }
    
        public void setUrgency(String urgency) {
            this.urgency = urgency;
        }
    
        public String getTags() {
            return tags;
        }
    
        public void setTags(String tags) {
            this.tags = tags;
        }
    }
    

    这是测试:

    public class ItemTest {
    
        @Test
        public void readItemTest() {
    
            final String itemXml = "<item>\n" +
                    "    <field1>Field1 Text</field1>\n" +
                    "    <field2>Field2 Text</field2>\n" +
                    "    <urgency id=\"UrgCaution\">Caution</urgency>\n" +
                    "    <tags>tag</tags>\n" +
                    "</item>";
    
            XmlMapper xm = new XmlMapper();
    
            try {
                Item myItem = xm.readValue(itemXml, Item.class);
    
                assert("Caution".equals(myItem.getUrgency()));
            } catch (IOException e) {
                e.printStackTrace();
                assert(false);
            }
    
        }
    }
    

    没有错误,没有问题,它按预期工作。

    Java 7

    我的依赖:

       <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.0</version>
        </dependency>
    
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.0</version>
        </dependency>
    
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.8.0</version>
        </dependency>
    

    【讨论】:

    • 很好奇。我将编辑主帖以包含完整的 XML 文档和所有注释。
    • @MarkAbersold 你在我回答后更新了 OP。没问题,我已经更新了。
    • 感谢您的更新。由于我无法更改 XML,这看起来像是我必须解决的问题。
    • @MarkAbersold 您可以尝试为 item 属性定义自定义反序列化器。这应该可以工作,但代码要多得多。
    猜你喜欢
    • 2013-11-19
    • 1970-01-01
    • 2013-12-10
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多