In the following code, I'm getting following exception. XmlAttribute/XmlValue not working properly which I'm not able to identify:-
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.
this problem is related to the following location:
at public java.util.Set nl.magnus.test.AddressComponent.getLocationTypeSet()
at nl.magnus.test.AddressComponent
My bean LocationType:-
@XmlRootElement(name = "type")
public class LocationType {
private Integer locationTypeId;
private String type;
private String status;
@Override
public String toString() {
return "LocationType [locationTypeId=" + locationTypeId + ", type=" + type + ", status=" + status + "]";
}
public Integer getLocationTypeId() {
return locationTypeId;
}
public void setLocationTypeId(Integer locationTypeId) {
this.locationTypeId = locationTypeId;
}
public String getType() {
return type;
}
@XmlAttribute(name = "type")
public void setType(String type) {
this.type = type;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
My bean AddressComponent:-
@XmlRootElement(name = "address_component")
public class AddressComponent {
private String longName;
private String shortName;
private Set<LocationType> locationTypeSet;
public String getLongName() {
return longName;
}
@XmlElement(name = "long_name")
public void setLongName(String longName) {
this.longName = longName;
}
public String getShortName() {
return shortName;
}
@XmlElement(name = "short_name")
public void setShortName(String shortName) {
this.shortName = shortName;
}
public Set<LocationType> getLocationTypeSet() {
return locationTypeSet;
}
@XmlAttribute(name = "type")
public void setLocationTypeSet(Set<LocationType> locationTypeSet) {
this.locationTypeSet = locationTypeSet;
}
@Override
public String toString() {
return "AddressComponent [longName=" + longName + ", shortName=" + shortName + ", locationTypeSet="
+ locationTypeSet + "]";
}
}
My Test class TestSmall:-
public class TestSmall {
public static void main(String[] args) {
try {
File file = new File("test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(AddressComponent.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
AddressComponent geoResponse = (AddressComponent) jaxbUnmarshaller.unmarshal(file);
System.out.println(geoResponse);
// Location latLong = geoResponse.getaResult().getGeometry().getLocation();
// System.out.println(latLong.getLatitude()+ ", "+latLong.getLongitude());
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
My xml test.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<address_component>
<long_name>BMC Colony</long_name>
<short_name>BMC Colony</short_name>
<type>neighborhood</type>
<type>political</type>
</address_component>
Please suggest me the required configuration.
In AddressComponent you mistakenly set type to an attribute @XmlAttribute(name = "type"). This should be an xml element instead.