在下面的代码中,我得到了以下异常。 XmlAttribute / XmlValue无法正常工作,我无法识别: -
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
我的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;
}
}
我的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();
}
}
}
我的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>
请建议我所需的配置。
答案 0 :(得分:4)
在AddressComponent中,您错误地将类型设置为属性@XmlAttribute(name =&#34; type&#34;)。这应该是一个xml元素。