Problem Deserializing Xmlrefid And Customizing JAXB Unmarshaller
Last updated on APRIL 15, 2018
Applies to:
Oracle Weblogic Server - Version 10.3.4 and laterInformation in this document applies to any platform.
Symptoms
JAXB types: CityArea, Country, GeographicDivision and DomainObject:
@XmlRootElement
public class CityArea extends GeographicDivision implements Serializable {
...
public class Country extends GeographicDivision implements Serializable {
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class GeographicDivision implements DomainObject {
@XmlID
private String id;
@XmlIDREF
private GeographicDivision parent;
public interface DomainObject {
public void setId(String id);
}
...
CityArea/Counry extends GeographicDivision, which has a id field, also a parent field. The parent is another instance of GeographicDivision.
A dummy CityArea instance is initialized like the following:
CityArea city = new CityArea();
...
city.setId(cityId);
...
city.setParent(state);
which the state is:
GeographicDivision state = new GeographicDivision();
...
state.setId("2345678");
...
state.setParent(country);
which the country is:
Country country = new Country();
...
country.setId("3456789");
...
country.setParent(null);
After the CityArea is marshalled, then unmarshalled, the following output is observed:
code:SNT, createdBy:AK, defaultName:San Jose, lastUpdatedBy:HL, reportName:San Jose, type:CITY, createdDt:Mon Apr 11 10:40:30 CST 2011, lastUpdatedDt:Mon Apr 11 10:40:30 CST 2011, id:123456, inactiveEffectiveDate:Mon Apr 11 10:40:30 CST 2011, parent:null, alternativeNames:[C-AN1, C-AN2, C-AN3, C-AN4, C-AN5, C-AN6, C-AN7, C-AN8, C-AN9], timeZones:[C-TZ1, C-TZ2, C-TZ3, C-TZ4, C-TZ5, C-TZ6, C-TZ7, C-TZ8, C-TZ9]utilDate : Mon Apr 11 10:40:30 CST 2011, postalCodes: [95131, 95132, 95133, 95134, 95135, 95136, 95137, 95138, 95139], customerGroupRegionCode: NAT, cy : true, defaultPostalCode: 95314, door : true, latitude : 35.23, longitude : 120.65, opZoneCode : Zone 1, shippingPoint : true, sqlDate : Mon Apr 11 10:40:30 CST 2011, unLocationCode : null
As can be seen, the parent bit is null.
When using custom IDResolver:
public Callable resolve(final String id, Class targetType) {
final String className=targetType.getCanonicalName();
return new Callable() {
public Object call() {
System.out.println("Id:" + id + ", targetType:" + className);
return createObjectWithId(className, id);
}
};
}
private Object createObjectWithId(String className, String idValue) {
Object object = null;
try {
Class classDefinition = Class.forName(className);
object = classDefinition.newInstance();
((DomainObject) object).setId(idValue);
} catch (InstantiationException e) {
System.out.println(e);
} catch (IllegalAccessException e) {
System.out.println(e);
} catch (ClassNotFoundException e) {
System.out.println(e);
}
return object;
}
}
The output is:
code:SNT, createdBy:AK, defaultName:San Jose, lastUpdatedBy:HL, reportName:San Jose, type:CITY, createdDt:Mon Apr 11 10:31:08 CST 2011, lastUpdatedDt:Mon Apr 11 10:31:08 CST 2011, id:123456, inactiveEffectiveDate:Mon Apr 11 10:31:08 CST 2011, parent: [ code:null, createdBy:null, defaultName:null, lastUpdatedBy:null, reportName:null, type:null, createdDt:null, lastUpdatedDt:null, id:2345678, inactiveEffectiveDate:null, parent:null, alternativeNames:null, timeZones:null], alternativeNames:[C-AN1, C-AN2, C-AN3, C-AN4, C-AN5, C-AN6, C-AN7, C-AN8, C-AN9], timeZones:[C-TZ1, C-TZ2, C-TZ3, C-TZ4, C-TZ5, C-TZ6, C-TZ7, C-TZ8, C-TZ9]utilDate : Mon Apr 11 10:31:08 CST 2011, postalCodes: [95131, 95132, 95133, 95134, 95135, 95136, 95137, 95138, 95139], customerGroupRegionCode: NAT, cy : true, defaultPostalCode: 95314, door : true, latitude : 35.23, longitude : 120.65, opZoneCode : Zone 1, shippingPoint : true, sqlDate : Mon Apr 11 10:31:08 CST 2011, unLocationCode : null
Observe that the parent bit is:
[ code:null, createdBy:null, defaultName:null, lastUpdatedBy:null, reportName:null, type:null, createdDt:null, lastUpdatedDt:null, id:2345678, inactiveEffectiveDate:null, parent:null, alternativeNames:null, timeZones:null]
id:2345678 is the state id set earlier. The rest are null as expected as the IDResolver doesn't set other fields.
However, with JAX-WS web service deployed on WebLogic, there is no way to gain access to JAXB unmashaller, thus cannot set the custom IDResolver.
Cause
Sign In with your My Oracle Support account |
|
Don't have a My Oracle Support account? Click to get started |
My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms