Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@XmlDiscriminatorNode/@XmlDescriminatorValue not working on WebLogic Server

Following are the classes I am using for create sub classes using MOXy JAXB conversion on WebLogic 10.3.2 version. I am using the EclipseLink 2.4.1 MOXy for generating the XML. I am unable to generate the type attribute in the following code. Let me know if I am doing anything wrong here.

I am using EclipseLink MOXy 2.4.1 and WebLogic 10.3.2 and MOXy 2.4.1 is configured in the WebLogic

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlDiscriminatorNode("@type")
public abstract class BaseEntity {

    private String firstName;
    private String lastName;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

}

Subclass

package forum13831189;

import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

@XmlDiscriminatorValue("xyz")
public class XyzEntity extends BaseEntity {

    public XyzEntity() {
        super();
    }

}

Another Sub Class

import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

@XmlDiscriminatorValue("Abc")
public class AbcEntity extends BaseEntity {
}

RESTful Web Service Class:

@GET
@Path("/xyz")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Representation getAccount() throws CPAException {
    Representation rep = new Representation();
    BaseEntity entity = new XyzEntity();
    entity.setFirstName("first-name");
    entity.setLastName("last-name");
    rep.setEntity(entity);
    return rep;
}

@XmlRootElement
static class Representation {
    private BaseEntity entity;

    public BaseEntity getEntity() {
        return entity;
    }

    public void setEntity(BaseEntity entity) {
        this.entity = entity;
    }
}

The above is generating the following XML.

<representation>
     <firstName>first-name</firstName>
      <lastName>last-name</lastName>
 </representation>

The attribute type is not generated in the above.


Thanks a lot. Yes, I missed jaxb.properties in the above. Also, Yes when I use the PUT or POST, when XML is de-serialized, it is not able to create the subclasses if @XmlSeeAlso is not present.

like image 450
user1896301 Avatar asked Nov 04 '22 08:11

user1896301


1 Answers

There are a couple items that may be causing you problems.

BaseEntity

By default a JAX-RS implementation creates a JAXBContext on the return type or parameter of the service method, in this case Represenatation. When processing the domain model the JAXB impl will also pull in referred types such as BaseEntity. It can't automatically pull in subclasses so we can use the @XmlSeeAlso annotation to reference those.

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlDiscriminatorNode("@type")
@XmlSeeAlso({AbcEntity.class, XyzEntity.class})
public abstract class BaseEntity {

    private String firstName;
    private String lastName;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

}

jaxb.properties

Also since @XmlDescriminatorNode/@XmlDescriminatorValue are MOXy extensions you need to make sure you specify MOXy as your JAXB provider. This is done by adding a file named jaxb.properties in the same package as your domain model with the following entry.

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

Below is a standalone example that mimics what your RESTful service does.

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

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Representation.class);

        Representation rep = new Representation();
        BaseEntity entity = new XyzEntity();
        entity.setFirstName("first-name");
        entity.setLastName("last-name");
        rep.setEntity(entity);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(rep, System.out);
    }

    @XmlRootElement
    static class Representation {
        private BaseEntity entity;

        public BaseEntity getEntity() {
            return entity;
        }

        public void setEntity(BaseEntity entity) {
            this.entity = entity;
        }
    }

}

Output

Below is the output from running the demo code. See that the type attribute is now present.

<?xml version="1.0" encoding="UTF-8"?>
<representation>
   <entity type="xyz">
      <firstName>first-name</firstName>
      <lastName>last-name</lastName>
   </entity>
</representation>

For More Information

  • Specifying EclipseLink MOXy as Your JAXB Provider
  • JAXB and Inheritance - MOXy Extension @XmlDescriminatorNode/@XmlDescrimintatorValue
  • Updating EclipseLink in WebLogic
like image 61
bdoughan Avatar answered Nov 11 '22 17:11

bdoughan