quiznos
quiznos

Reputation: 13

JAXB-RI Marshalling generic list gives incorrect output

We have a complicated structure of classes that we are trying to marshal to an xml file using JAXB-RI. The marshalling seems to work correctly with Spring's jaxb2Marshaller but not with the jaxb-ri, which is what we are trying to use. (We're using Java 6 and jaxb-2.1.13)

This is an example of the output we currently see after marshalling with JAXB-RI:

<?xml version="1.0" encoding="UTF-8"?>
     <specificCompanyList>
       <org>com.ourcompany.etc.etc.TypeOfCompany@56cb0eed</org>
       <org>com.ourcompany.etc.etc.TypeOfCompany@3125a57</org>
       ....
     </specificCompanyList>

This is what we want to see:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<specificCompanyList>
  <org id="123456" name=" COMPANY NAME 1"/>
  <org id="098765" name=" COMPANY NAME 2"/>
  ....
</specificCompanyList>

Here is some info about our class structure. Apologies if it's confusing -- I want to lay everything out on the table. Class names and paths have been changed and shortened as well.

Class that becomes the root element:

@XmlRootElement(name="specificCompanyList")
public class SpecificCompanyLookup extends BaseCompanyLookup<TypeOfCompany> {
    public SpecificCompanyLookup() {}
    ....
}

BaseCompanyLookup:

@XmlAccessorType(XmlAccessType.FIELD)
abstract class BaseCompanyLookup<T extends OrgNode> implements Lookup {
    @XmlElement(name="org")
    final Set<T> companyList = new TreeSet<T>(.....);
}

OrgNode:

@XmlAccessorType(XmlAccessType.FIELD)
classOrgNode extends BaseLookupItem {
}

BaseLookupItem:

@XmlAccessorType(XmlAccessType.FIELD)
public class BaseLookupItem {

    @XmlAttribute(required = true)
    protected String id;

    @XmlAttribute(required = true)
    protected String name;

    ....
}

A class that extends OrgNode:

class TypeOfCompany extends OrgNode {
    ....
}

So: BaseLookupItem -> OrgNode -> TypeOfCompany

Does anyone know what is causing this bad output? How do we make the JAXB-RI marshaller generate the output that we need?

EDIT: We found the solution. This happened when we moved to WebLogic 12c, which has switched its JAXB default to the EclipseLink MOXy implementation. That implementation appears to have a bug for the case I described here. Following the instructions to switch to the Glassfish JAXB RI fixed this issue for us. Here are those instructions: http://docs.oracle.com/cd/E24329_01/web.1211/e24964/data_types.htm#CIHBHDGI

Upvotes: 1

Views: 429

Answers (1)

Denise
Denise

Reputation: 201

I confirmed this is in fact a bug in MOXy and have opened the following bug to track the issue https://bugs.eclipse.org/bugs/show_bug.cgi?id=410001

I also confirmed that adding the @XmlSeeAlso annotation like this would be a workaround for this case.

@XmlSeeAlso(TypeOfCompany.class)
public class SpecificCompanyLookup extends BaseCompanyLookup<TypeOfCompany> {
    public SpecificCompanyLookup() {}
}

Upvotes: 1

Related Questions