Home > Net >  UnrecognizedPropertyException for a ghost attribute in XML
UnrecognizedPropertyException for a ghost attribute in XML

Time:04-03

I'm currently writing a program to display weather data from the MSC. I'm using Jackson 2.9.6 (as a different library didn't work on other versions) and the XML extension (also in Jackson 2.9.6) to access and display the data provided online, which is in XML format.

I'm running into problems with representing the times given from the API. My code works until it hits the month, it seems.

This is what the test date data looks like.

<dateTime name="xmlCreation" zone="UTC" UTCOffset="0">
        <year>2022</year>
        <month name="April">04</month>
        <day name="Saturday">02</day>
        <hour>20</hour>
        <minute>50</minute>
        <timeStamp>20220402205000</timeStamp>
        <textSummary>Saturday April 02, 2022 at 20:50 UTC</textSummary>
</dateTime>

And this is what my POJO class for the data looks like.

package city_weather;
import com.fasterxml.jackson.dataformat.xml.annotation.*;

public class DateTime {
    @JacksonXmlProperty(isAttribute = true)
    private String name;
    @JacksonXmlProperty(isAttribute = true)
    private String zone;
    @JacksonXmlProperty(isAttribute = true)
    private String UTCOffset;
    @JacksonXmlProperty(localName = "year")
    private int year;
    @JacksonXmlProperty(localName = "month")
    private Month month;
    @JacksonXmlProperty(localName = "day")
    private Day day;
    private int hour;
    private int minute;
    private String timeStamp;
    private String textSummary;
}

class Month {
    @JacksonXmlProperty(localName = "month")
    public int month;
    public String name;

}

class Day {
    @JacksonXmlProperty(localName = "day")
    public int day;
    public String name;
}

I tried to add the tags and annotations across the code, but it didn't seem to work. Also, my first few attempts featured the month/day not as separate classes but as variables Month, Day, MonthName, and DayName. I'm not sure how I'm supposed to ignore these attributes and just carry on.

When I run my code (which is just using the XMLMapper to map the XML file to an instance of that DateTime class), it doesn't work. Here's the error it produced :

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "" (class city.weather.Month), not marked as ignorable (2 known properties: "month", "name"])
 at [Source: (File); line: 3, column: 35] (through reference chain: city.weather.DateTime["month"]->city.weather.Month[""])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:136)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2902)
    at Tester.main(Tester.java:8)

This stumped me because clearly you can see that there isn't any attributes listed (just a "" field, which tells me nothing). I tried to find documentation but there wasn't much I could find.

CodePudding user response:

Fixed! For this I simply added @JsonIgnoreProperties({""}) in the start of the classes Month and Day.

CodePudding user response:

Using @JsonIgnoreProperties({""}) on the Month and Day classes (as written in your own answer) surely works.

Another (and in my opinion better) way would be to annotate the month property of class Month and the day property of class Day with @JacksonXmlText instead of @JacksonXmlProperty(...). By doing so these properties would actually receive the day and month number from XML.

  • Related