Jackson XML 注释:从具有属性的 XML 元素中提取单个字符串值

Mar*_*old 3 java xml pojo jackson

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

<title>Title Here</title>
Run Code Online (Sandbox Code Playgroud)

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

<urgency id="UrgCaution">Caution</urgency>
Run Code Online (Sandbox Code Playgroud)

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

public class Item {
    @JacksonXmlProperty(localName = "urgency")
    private String urgency;
}
Run Code Online (Sandbox Code Playgroud)

但这会导致以下错误:

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] 
Run Code Online (Sandbox Code Playgroud)

“Security”是稍后出现在文档中的称为“tags”的 xml 属性的文本。

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

public class Item {
    @JacksonXmlProperty(localName = "urgency")
    @JacksonXmlText
    private String urgency;
}
Run Code Online (Sandbox Code Playgroud)

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

这是我正在使用的完整类和 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>
Run Code Online (Sandbox Code Playgroud)

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

根类:

@JacksonXmlRootElement(localName = "rss")
public class FeedRoot {
    @JacksonXmlProperty(localName = "channel")
    private FeedChannel channel;
}
Run Code Online (Sandbox Code Playgroud)

频道类:

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

项目类(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
}
Run Code Online (Sandbox Code Playgroud)

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

"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])"
Run Code Online (Sandbox Code Playgroud)

fre*_*lys 5

要有这个

<urgency id="UrgCaution">Caution</urgency>
Run Code Online (Sandbox Code Playgroud)

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

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