Thursday, January 14, 2016

No @XmlRootElement annotation

If you get "Expected elements are (none)" in a javax.xml.bind.UnmarshalException, you probably don't have an @XmlRootElement annotation in the Java object that your code is unmarshalling to.

For example, this error:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"customer"). Expected elements are (none)
when unmarshalling this XML:
<?xml version="1.0" encoding="UTF-8"?>
<customer id="100">
    <age>23</age>
    <name>Daffy Duck</name>
</customer>
to this class, is fixed by adding the annotation @XmlRootElement to the class so it looks like this:
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customerType", propOrder = {
        "id",
        "name",
        "age"
})
public class Customer {

    @XmlElement
    String name;
    @XmlElement
    int age;
    @XmlAttribute
    int id;

}

2 comments:

  1. Code Mule: No @Xmlrootelement Annotation >>>>> Download Now

    >>>>> Download Full

    Code Mule: No @Xmlrootelement Annotation >>>>> Download LINK

    >>>>> Download Now

    Code Mule: No @Xmlrootelement Annotation >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete

DBeaver vs. SQL Developer: DBeaver fail!

DBeaver claims to be the best database editor. Thanks to some bad UI, something as simple as switching the database schema in the SQL editor...