using JAXB 2.2.5b, and having
#XmlSeeAlso({BeanView.class, ViewField.class, ViewPanel.class, ViewTab.class, ElementRef.class})
#XmlRootElement
public abstract class ViewElement {
#XmlID
#XmlAttribute
public String getId() {
return id;
}
}
#XmlRootElement(name="ref")
public class ElementRef extends ViewElement{
private ViewElement target;
#XmlElement
#XmlIDREF
public ViewElement getTarget() {
return target;
}
public void setTarget(ViewElement target) {
this.target = target;
}
}
Many class extends the abstract ViewElement
#XmlRootElement
public class ViewField extends ViewElement{
}
#XmlRootElement
public class ViewPanel extends ViewElement{
}
in the XML the ElementRef target element is repeated instead of keeping only the id ref
<ref id="mvmCredit" >
<target xsi:type="viewField" valueExpr="" row="0" ... all ViewField attributes >
</target>
</ref>
and during the unmarchaling of the XML model the target is null (the restoration failed)
An upgrade to JAXB 2.2.7 fix the problem
Related
I want to find beans at runtime depending on user input, so using io.quarkus.arc.Arc.
However, I cannot figure out how to get beans with custom annotation in a reasonable way. Only got something working by implementing the annotation interface like:
InstanceHandle<Service> instanceHandleA = Arc.container()
.instance(Service.class, new SupportsJobTypeImpl(JobType.A));
InstanceHandle<Service> instanceHandleB = Arc.container()
.instance(Service.class, new SupportsJobTypeImpl(JobType.B));
class SupportsJobTypeImpl implements SupportsJobType {
JobType requestedJobType;
public SupportsJobTypeImpl(JobType requestedJobType) {
this.requestedJobType = requestedJobType;
}
#Override
public JobType value() {
return requestedJobType;
}
#Override
public Class<? extends Annotation> annotationType() {
return SupportsJobType.class;
}
}
Is there a way to get instanceHandleA, instanceHandleB with less cumbersome verbose code like SupportsJobTypeImpl?
Background info about my test code:
public enum JobType {
A,B
}
#Qualifier
#Retention(RUNTIME)
#Target({METHOD, FIELD, PARAMETER, TYPE})
public #interface SupportsJobType {
JobType value();
}
#ApplicationScoped
#SupportsJobType(JobType.A)
public class ServiceForA extends Service {
}
#ApplicationScoped
#SupportsJobType(JobType.B)
public class ServiceForB extends Service {
}
UPDATE: Tried to get it working by including SupportsJobTypeImpl in the SupportsJobType annotation interface (like in the example from https://docs.jboss.org/cdi/api/2.0/javax/enterprise/util/AnnotationLiteral.html):
#Qualifier
#Retention(RUNTIME)
#Target({METHOD, FIELD, PARAMETER, TYPE})
public #interface SupportsJobType {
JobType value();
public abstract class Literal extends AnnotationLiteral<SupportsJobType> implements SupportsJobType {
JobType requestedJobType;
public Literal() {
}
#Override
public JobType value() {
return requestedJobType;
}
#Override
public Class<? extends Annotation> annotationType() {
return SupportsJobType.class;
}
}
}
however, I get no bean instance from ArC by:
InstanceHandle<Service> instanceHandleA = Arc.container().instance(Service.class, new SupportsJobType.Literal() {
public JobType value() {
return JobType.A;
}
});
UPDATE2: also not working:
#Qualifier
#Retention(RUNTIME)
#Target({METHOD, FIELD, PARAMETER, TYPE})
public #interface SupportsJobType {
JobType value();
class Literal extends AnnotationLiteral<SupportsJobType> implements SupportsJobType {
JobType requestedJobType;
public Literal(JobType requestedJobType) {
this.requestedJobType = requestedJobType;
}
#Override
public JobType value() {
return requestedJobType;
}
#Override
public Class<? extends Annotation> annotationType() {
return SupportsJobType.class;
}
}
}
I get no bean instance from ArC by:
InstanceHandle<Service> instanceHandleA = Arc.container().instance(Service.class, new SupportsJobType.Literal(JobType.A));
As far as I can tell, what you're doing is exactly what you should do. It is idiomatic for implementations of annotation types to be nested in the annotation type itself, be called Literal and extend the AnnotationLiteral class. For example (writing from memory):
#Qualifier
#Retention(RUNTIME)
#Target({METHOD, FIELD, PARAMETER, TYPE})
public #interface SupportsJobType {
JobType value();
class Literal extends AnnotationLiteral<SupportsJobType> implements SupportsJobType {
private final JobType value;
public Literal(JobType value) {
this.value = value;
}
#Override
public JobType value() {
return value;
}
}
}
Turns out I was stumbling over a small detail like mentioned in https://stackoverflow.com/a/66980506/753724
which means I simply had to add #Unremovable to beans resulting in:
#ApplicationScoped
#Unremovable
#SupportsJobType(JobType.A)
public class ServiceForA extends Service {
}
#ApplicationScoped
#Unremovable
#SupportsJobType(JobType.B)
public class ServiceForB extends Service {
}
With the slightly adapted Annotation class (only new static method):
#Qualifier
#Retention(RUNTIME)
#Target({METHOD, FIELD, PARAMETER, TYPE})
public #interface SupportsJobType {
JobType value();
class Literal extends AnnotationLiteral<SupportsJobType> implements SupportsJobType {
JobType requestedJobType;
public Literal(JobType requestedJobType) {
this.requestedJobType = requestedJobType;
}
public static Literal of(JobType value) {
return new Literal(value);
}
#Override
public JobType value() {
return requestedJobType;
}
#Override
public Class<? extends Annotation> annotationType() {
return SupportsJobType.class;
}
}
}
the beans were found as expected by:
InstanceHandle<Service> instanceHandleA = Arc.container().instance(Service.class, SupportsJobType.Literal.of(JobType.A));
InstanceHandle<Service> instanceHandleB = Arc.container().instance(Service.class, SupportsJobType.Literal.of(JobType.B));
Does XStream handle JAXB #XmlTransient attributes by default? XStream seems to be ignoring the #XmlTransient attribute & serializing the field anyway.
In the sample code below. ExampleClass2 is getting serialized even though I don't want it to be. Further details are that these classes are being populated by OpenJPA.
XStream Code
XStream _x0 =null;
_x = XStreamImpl.getInstance();
_x.toXML(_object)
Class I want to serialize
#DataCache
#Entity
public class ExampleClass implements Serializable {
private short defaultOption;
private int primaryKey;
private short orderId;
#XmlTransient
private ExampleClass2 _exampleClass2;
#XmlTransient
public ExampleClass2 getTblPpwsCommCfgCombo() {
return _exampleClass2;
}
#XmlTransient
public void setExampleClass2(ExampleClass2 _exampleClass2) {
this._exampleClass2 = _exampleClass2;
}
public short getDefaultOption() {
return defaultOption;
}
public void setDefaultOption(short defaultOption) {
this.defaultOption = defaultOption;
}
public short getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(int primaryKey) {
this.primaryKey = primaryKey;
}
public short getOrderId() {
return orderId;
}
public void setOrderId(short orderId) {
this.orderId = orderId;
}
}
You can use the #Transient annotation or transiet key word:
#Transient
private ExampleClass2 _exampleClass2;
~
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.
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
First of all a small example. The class ReferencingEntity holds a reference to the abstract class AbstractEntity. There are two implementations fo this class:
#XmlRootElement
public abstract class AbstractEntity {
#XmlID
private String id;
}
#XmlRootElement
public class EntityImpl1 extends AbstractEntity {
}
#XmlRootElement
public class EntityImpl2 extends AbstractEntity {
}
#XmlRootElement
public class ReferencingEntity {
#XmlIDREF
private AbstractEntity entity;
}
There is no problem marshalling an instance of ReferencingEntity (except that the concrete type is not present in xml), but when trying to unmarshal the xml representation, the descriptor is missing to determine the concrete implementation.
Currently I'm using an XmlAdapter to set all non-id fields null, but it would be better to use #XmlID if possible. Any ideas?
UPDATE:
I'm using RESTEasy in JBoss 6.1.0.Final and the provider creates the context as follows:
ContextResolver<JAXBContextFinder> resolver = providers.getContextResolver(JAXBContextFinder.class, mediaType);
JAXBContextFinder finder = resolver.getContext(type);
if (finder == null)
{
if (reader) throw new JAXBUnmarshalException("Could not find JAXBContextFinder for media type: " + mediaType);
else throw new JAXBMarshalException("Could not find JAXBContextFinder for media type: " + mediaType);
}
JAXBContext context = finder.findCachedContext(type, mediaType, annotations);
Below is my initial answer to your question. I imagine it will evolve as I better understand your use case.
ABOUT #XmlID/#XmlIDREF
Every instance referenced from a field/property annotated with #XmlIDREF also needs to be referenced via containment. I'll use the class below in this example.
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class Root {
private AbstractEntity abstractEntity;
private ReferencingEntity referencingEntity;
public AbstractEntity getAbstractEntity() {
return abstractEntity;
}
public void setAbstractEntity(AbstractEntity abstractEntity) {
this.abstractEntity = abstractEntity;
}
public ReferencingEntity getReferencingEntity() {
return referencingEntity;
}
public void setReferencingEntity(ReferencingEntity referencingEntity) {
this.referencingEntity = referencingEntity;
}
}
REGARDING INHERITANCE
JAXB (JSR-222) implementations can't automatically discover subclasses, so you will need to be sure that the JAXBContext is aware of them. One way to accomplish this is to use the #XmlSeeAlso annotation on the parent class to point at the child classes.
import javax.xml.bind.annotation.*;
#XmlSeeAlso({EntityImpl1.class, EntityImpl2.class})
#XmlAccessorType(XmlAccessType.FIELD)
public abstract class AbstractEntity {
#XmlID
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
DEMO CODE
Demo
package forum12111815;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum12111815/input.xml");
Root root = (Root) unmarshaller.unmarshal(xml);
System.out.println(root.getAbstractEntity().getClass());
System.out.println(root.getAbstractEntity() == root.getReferencingEntity().getEntity());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<abstractEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="entityImpl2">
<id>123</id>
</abstractEntity>
<referencingEntity>
<entity>123</entity>
</referencingEntity>
</root>
Output
class forum12111815.EntityImpl2
true
<?xml version="1.0" encoding="UTF-8"?>
<root>
<abstractEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="entityImpl2">
<id>123</id>
</abstractEntity>
<referencingEntity>
<entity>123</entity>
</referencingEntity>
</root>
FOR MORE INFORMATION
http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html
http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html
Here is what I have so far to marshall my POJO using JAXB :
#XmlRootElement
public class Local {
private Entity entity;
public void setEntity(Entity entity) {
this.entity = entity;
}
#XmlElement
public Entity getEntity() {
return entity;
}
}
and
#XmlRootElement
public class Entity {
private String name;
private String comment;
public void setName(String name){
this.name = name;
}
#XmlAttribute
public String getName(){
return this.name;
}
public void setComment...
#XmlAttribute
public void getComment...
}
With that, I get something like this:
<local>
<entity name="" comment=""></entity>
</local>
However, I would prefer to have the name attribute as an attribute of the local:
<local entityName="" entityComment=""></local>
Is the XmlJavaTypeAdapter a good way to begin with?
Thanks,
Alex
There are a couple of different options to handle this use case:
Option #1 - XmlAdapter (Any JAXB implementation)
You could use an XmlAdapter for this use case. This will work as long as only one attribute value comes from the Entity object:
EntityAdapter
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class EntityAdapter extends XmlAdapter<String, Entity>{
#Override
public String marshal(Entity entity) throws Exception {
if(null == entity) {
return null;
}
return entity.getName();
}
#Override
public Entity unmarshal(String name) throws Exception {
Entity entity = new Entity();
entity.setName(name);
return entity;
}
}
Local
The XmlAdapter is linked with the field/property using the #XmlJavaTypeAdapter annotation:
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
#XmlRootElement
public class Local {
private Entity entity;
public void setEntity(Entity entity) {
this.entity = entity;
}
#XmlAttribute
#XmlJavaTypeAdapter(EntityAdapter.class)
public Entity getEntity() {
return entity;
}
}
For More Information
http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
http://blog.bdoughan.com/2010/12/jaxb-and-immutable-objects.html
Option #2 - #XmlPath (EclipseLink JAXB (MOXy)
Alternatively if you are using EclipseLink JAXB (MOXy), the you could use the #XmlPath extension. This is useful with the Entity object corresponds to multiple XML attributes:
Local
Specifying the XPath "." indicated that the child contents will be written into the parent element
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;
#XmlRootElement
public class Local {
private Entity entity;
public void setEntity(Entity entity) {
this.entity = entity;
}
#XmlPath(".")
public Entity getEntity() {
return entity;
}
}
Entity
public class Entity {
private String name;
private String comment;
public void setName(String name){
this.name = name;
}
#XmlAttribute(name="entityName")
public String getName(){
return this.name;
}
public void setComment(String comment){
this.comment = comment;
}
#XmlAttribute(name="entityComment")
public String getComment(){
return this.comment;
}
}
For More Information
http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html
http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html
http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html