JaxB fixed value annotation doesn't work
Answered
Hi,
I have XSD schema like this:
<xsd:annotation>
<xsd:appinfo>
<jaxb:globalBindings fixedAttributeAsConstantProperty="true" />
</xsd:appinfo>
...
<xsd:complexType name="something_here">
<xsd:complexContent>
<xsd:restriction base="genericObject">
<xsd:attribute name="type" type="xsd:string" fixed="FixedValue" use="required"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
I use Tools/JAXB/Generate Java Code From Xml Schema Using JAXB
By default this is checked and I leav it this way:
- Make generated files read-only
- Add necessary libraries
- Do not generate header
And when I click Ok it generates classes but...
This fixed value is not fixed. Why?
Please sign in to leave a comment.
Hi.
Could you please provide example project to reproduce a problem?
Hi,
Here is complete schema. I don't get fixed value for object007 (it should be constant "FIXED_VALUE_5" but it is not in class Object007).
Also, not only fixed value are not fixed but I have problem with List.
For example:
ObjectFactory factory = new ObjectFactory();
Object007 obj = factory.createObject007();
obj.setId("id1");
Medatada006 meta = factory.createMedatada006();
Attr005 attr = factory.createAttr005();
attr.setName("aaa");
attr.setValue("bbb");
meta.getAttr().add(attr);
Event012 event = factory.createEvent012();
event.setObject(obj);
event.setAny(meta);
JAXBElement<GenericEvent> genericEvent = factory.createEvent(event);
JAXBContext jaxbContext = JAXBContext.newInstance( Event012.class );
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
StringWriter sw = new StringWriter();
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
jaxbMarshaller.marshal(event, sw);
System.out.println(sw.toString());
And I get this:
[EL Warning]: 2018-12-14 18:09:40.035--XMLAnyObjectMappingNodeValue: The undefined document root element of a referenced object [XMLDescriptor(test2.model.Medatada006 --> [])] is ignored during marshalling with an any collection|object mapping.
<?xml version="1.0" encoding="UTF-8"?><event xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Event012"><object id="id1" xsi:type="object007"/></event>
Because, Medatada006 and Event012 didn't created properly, there is no information that medatada is also element wripping attrributes element.
Also Medatada006 has a list of attributes even if there is ENUMERATION in schema for attribute.
- XML Schema