JAXB unmarshalling not working as expected

I am trying to use JAXB

to convert XML to Object , my XML looks like this:

<entityResource>
   <Item xsi:type="objectPermissionImpl">
      <permissionMask>0</permissionMask>
      <permissionRecipient xsi:type="roleImpl">
        <externallyDefined>false</externallyDefined>
        <roleName>ROLE_USER</roleName>
      </permissionRecipient>
      <URI>repo:/public/adhoc/topics/JSDiagnosticTopic</URI>
   </Item>
   <Item xsi:type="objectPermissionImpl">
      <permissionMask>0</permissionMask>
      <permissionRecipient xsi:type="roleImpl">
        <externallyDefined>false</externallyDefined>
        <roleName>ROLE_ADMINISTRATOR</roleName>
      </permissionRecipient>
      <URI>repo:/public/adhoc/topics/JSDiagnosticTopic</URI>
   </Item>
</entityResource>

      

So, I created 3 java classes: EntityResource.java, Item.java and PermissionRecipient.java as shown below:

EntityResource.java

import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="entityResource")
public class EntityResource {

    List<Item> ls_Item;

    public EntityResource() {
    }

    public List<Item> getLs_Item() {
        return ls_Item;
    }

    @XmlElement(name="Item")
    public void setLs_Item(List<Item> ls_Item) {
        this.ls_Item = ls_Item;
    }

    @Override
    public String toString() {
        return "EntityResource [ls_Item=" + ls_Item + "]";
    }

}

      

Item.java

package model;

import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Item")
public class Item {

    int permissionMask;
    List<PermissionRecipient> ls_permissionRecipient;
    String URI;

    public Item() {

    }

    public int getPermissionMask() {
        return permissionMask;
    }

    @XmlElement(name="permissionMask")
    public void setPermissionMask(int permissionMask) {
        this.permissionMask = permissionMask;
    }

    public List<PermissionRecipient> getLs_permissionRecipient() {
        return ls_permissionRecipient;
    }

    @XmlElement(name="permissionRecipient")
    public void setLs_permissionRecipient(
            List<PermissionRecipient> ls_permissionRecipient) {
        this.ls_permissionRecipient = ls_permissionRecipient;
    }

    public String getURI() {
        return URI;
    }

    @XmlElement(name="URI")
    public void setURI(String uRI) {
        URI = uRI;
    }

    @Override
    public String toString() {
        return "Item [permissionMask=" + permissionMask
                + ", ls_permissionRecipient=" + ls_permissionRecipient
                + ", URI=" + URI + "]";
    }





}

      

PermissionRecipient.java

package model;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="permissionRecipient")
public class PermissionRecipient {

    String roleName;
    boolean externallyDefined;

    public PermissionRecipient() {

    }

    public boolean isExternallyDefined() {
        return externallyDefined;
    }

    @XmlAttribute(name="externallyDefined")
    public void setExternallyDefined(boolean externallyDefined) {
        this.externallyDefined = externallyDefined;
    }

    public String getRoleName() {
        return roleName;
    }

    @XmlAttribute(name="roleName")
    public void setRoleName(String rolename) {
        this.roleName = rolename;
    }

    @Override
    public String toString() {
        return "PermissionRecipient [externallyDefined=" + externallyDefined
                + ", roleName=" + roleName + "]";
    }


}

      

Everything worked and I got an EntityResource containing an Item, but the permissionRecipient attribute of the Item EntityResource does not contain its attributes (roleName and externallyDefined)!

My unmarshalling code is here:

JAXBContext jaxbContext = JAXBContext
                    .newInstance(EntityResource.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            EntityResource resourceDescriptors = (EntityResource) jaxbUnmarshaller
                    .unmarshal(conn.getInputStream());// conn is an HttpURLConnection

      

The toString()

Element function returned results:

[Item 
[permissionMask=0, 
permissionRecipient=PermissionRecipient [externallyDefined=false, roleName=null], 
URI=repo:/public/adhoc/topics/JSDiagnosticTopic], 

Item 
[permissionMask=0, 
permissionRecipient=PermissionRecipient [externallyDefined=false, roleName=null], 
URI=repo:/public/adhoc/topics/JSDiagnosticTopic]]

      

how can you mark [externallyDefined=false, roleName=null]

in each item why? what bug did I bit? Thanks if anyone can help me solve it, best wishes.

+3


source to share


1 answer


You roleName

and is externallyDefined

displayed @XmlAttribute

instead of @XmlElement

.

Debugging tip



When your object model is not being overridden as expected, populate it and marshal it to XML, and then compare the output to your input.

+4


source







All Articles