XJC - Add Lombok Annotation on top of every Generated XSD class and remove Setters Getters - xjc

I am working on jaxb2-maven-plugin, I am generating Java POJOs from XSD.
I need to add #Data annotation on top of the class without setters getters.
The output file should look like this.
#Data
public class Employee {
#JsonProperty("name")
private Name name;
#JsonProperty("joindate")
private String joindate;
#JsonProperty("payload")
private String payload;
}
My XSD is like this
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="name">
<xs:complexType>
<xs:sequence>
<xs:element name="lastname" type="xs:string" />
<xs:element name="firstname" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="hiredate" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I need to add annotation on Name.java as well. I have around 100 XSDs. I need to do this for all XSDs

Related

XJC -jaxb2-maven-plugin-Generated Getter Setter methods does not follow Java conventions

I am working on jaxb2-maven-plugin, I am generating Java POJOs from XSD.
I have an XSD like this
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="projects">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="project">
<xs:complexType>
<xs:sequence>
<xs:element name="product" type="xs:string" />
<xs:element name="id" type="xs:unsignedByte" />
<xs:element name="**PRIce**" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
When I generate the POJOs using the above it generated the getters/setters for field PRIce like this.
public class Project {
#XmlElement(name = "PRIce", required = true)
protected String prIce;
public String getPRIce() {
return prIce;
}
public void setPRIce(String value) {
this.prIce = value;
}
}
But we need the output like this.
public class Project {
#XmlElement(name = "PRIce", required = true)
protected String prIce;
public String getPrIce() {
return prIce;
}
public void setPrIce(String value) {
this.prIce = value;
}
}

MappingJackson2XmlHttpMessageConverter creates extra/duplicate xml elements during marshalling

i am using xjc to create jaxb binding classes from an xsd... and using MappingJackson2XmlHttpMessageConverter to convert the objects to xml.. but what i am seeing is, when i have a list in the binding file, the converter creates a duplicate xml element..
XSD:
<xs:complexType name="orderSubmitType">
<xs:all>
<xs:element name="orderId" type="xs:string" />
<xs:element name="giftCards" type="giftCards"/>
</xs:all>
</xs:complexType>
<xs:complexType name="giftCards">
<xs:sequence>
<xs:element name="giftCard" type="giftCard" minOccurs="0" maxOccurs="10"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="giftCard">
<xs:all>
<xs:element name="cardNumber" type="xs:string" />
<xs:element name="pinNumber" type="xs:string" />
</xs:all>
</xs:complexType>
JAVA:
public class TestRestTemplateConfiguration {
public static RestTemplate getTestClientRestTemplate() {
if (INSTANCE == null) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new TestClientResponseErrorHandler());
ClientHttpRequestInterceptor loggingInterceptor = new TestClientLoggingInterceptor();
restTemplate.setInterceptors(Collections.singletonList(loggingInterceptor));
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
MappingJackson2XmlHttpMessageConverter jacksonXmlConverter = new MappingJackson2XmlHttpMessageConverter();
restTemplate.setMessageConverters(Arrays.asList(jacksonConverter,jacksonXmlConverter));
INSTANCE = restTemplate;
}
return INSTANCE;
}
}
final xml that is created is ..
<?xml version="1.0" encoding="UTF-8"?>
<OrderSubmitType>
<orderId>111111</orderId>
<giftCards>
<giftCard>
<giftCard>
<cardNumber>123456790</cardNumber>
<pinNumber>1111</pinNumber>
</giftCard>
</giftCard>
</giftCards>
</OrderSubmitType>
as you can see... there is a extra
<giftcard></giftcard> elements...
can anyone tell me what is the mistake that i am doing...
thanks...

Complex XPaths using EclipseLink MOXy and JAXB

I am currently using EclipseLink MOXy 2.4.1 and trying to update an element's value via XPaths.
The input used is :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<expenseReport>
<user>
<userName>Sanaulla</userName>
</user>
<items>
<item>
<itemName>Seagate External HDD</itemName>
<purchasedOn>August 24, 2010</purchasedOn>
<amount>6776.5</amount>
</item>
<item>
<itemName>External HDD</itemName>
<purchasedOn>August 24, 2010</purchasedOn>
<amount>677336.5</amount>
</item>
</items>
</expenseReport>
The XSD is
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="expenseReport" type="ExpenseT"/>
<xs:complexType name="ExpenseT">
<xs:sequence>
<xs:element name="user" type="UserT"/>
<xs:element name="items" type="ItemListT"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="UserT">
<xs:sequence>
<xs:element name="userName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ItemListT">
<xs:sequence>
<xs:element name="item" type="ItemT" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ItemT">
<xs:sequence>
<xs:element name="itemName" type="xs:string"/>
<xs:element name="purchasedOn" type="xs:string"/>
<xs:element name="amount" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
And my Java code is :
JAXBContext context = JAXBContext.newInstance("sample.jaxb.xsd");
JAXBElement<ExpenseT> element = (JAXBElement<ExpenseT>)unmarshaller.unmarshal(new File("resources/sample_before.xml"));
ExpenseT expenseReport = element.getValue();
context.setValueByXPath(expenseReport, "items/item[1]/amount/text()", null, "100.11");
/** Marshalling code **/
I have my java classes created from the XSD using Moxy's tool "jaxb-compiler.cmd".
As you can see from the above code, I am trying to set the amount of 1st item to "100.11". But it doesn't set the value, nor does it throw any exception.
I tried setting the username above using XPath like this
context.setValueByXPath(expenseReport, "user/userName/text()", null, "ABC");
And, it does set/update the value properly.
Can you please advise on how can I set values for complex XPaths with Predicates? Something like this :
items/item[itemName='Seagate External HDD']/amount
We recently fixed a bug in our EclipseLink 2.4.2 and 2.5.0 streams related to setting values in which the XPath contains a postional indicator (ie "foo[1]/bar[2]/text()"). You can download a nightly build from the following link:
http://www.eclipse.org/eclipselink/downloads/nightly.php
You can use predicates in mappings with #XmlPath (see: http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html), but we currently don't supports this with our setValueByXPath call. Could you open an enhancement

XSD context-dependent types; wrapper types

I am writing a schema for XML documents like this:
<workbook>
<worksheet>
<column/>
</worksheet>
</workbook>
Where each node can be wrapped into the env element (e.g.)
<workbook>
<env>
<worksheet>
<column/>
</worksheet>
</env>
</workbook>
or
<workbook>
<worksheet>
<env>
<column/>
</env>
</worksheet>
</workbook>
So, depending on the location of the env element, it can have different children (env-child of workbook must have worksheet children and env-child of worksheet must have column children)
When I am declaring env elements as nested, I get multiply-defined element errors.
How could I write xsd for such document, or is it at all possible?
Thanks in advance!
Yes, it is possible, because you can have elements with the same name env but with different types (structure) in different places - something linke this:
<xs:element name="workbook">
<xs:complexType>
<xs:choice>
<xs:element name="env">
<xs:complexType>
<xs:sequence>
<xs:element ref="worksheet"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element ref="worksheet"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="worksheet">
<xs:complexType>
<xs:choice>
<xs:element name="env">
<xs:complexType>
<xs:sequence>
<xs:element ref="column"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element ref="column"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="column" type="xs:string">
</xs:element>

BizTalk generated XSD fails to validate the XML its derived from

I've got an XML response from another system, but no XSD, so I used the Create Schema option to generate one.
I then added the XSD to my BizTalk 2006 R2 project and set its "Input Instance Filename" property to the original XML message.
Tried the "Validate Instance" option and it FAILS ???, with a couple of errors like so ...
error BEC2004: The xsi:type attribute value 'http://www.w3.org/2001/XMLSchema:int' is not valid for the element 'http://www.aniteps.com/xml/schemas/awm/images4:NumberOfErrors', either because it is not a type validly derived from the type in the schema, or because it has xsi:type derivation blocked.
How the heck can the XML fail that was used to generate the XSD ?
The XML example I have is ...
<?xml version="1.0" encoding="utf-8"?>
<ImportIndexDocumentResponse
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.aniteps.com/xml/schemas/awm/images4">
<HasErrors>false</HasErrors>
<NumberOfErrors xsi:type="xsd:int">0</NumberOfErrors>
<ErrorDescription xsi:type="xsd:string">No exception ocurred.</ErrorDescription>
<ErrorNumber xsi:type="xsd:int">0</ErrorNumber>
<FailedItems>
<Item>
<OriginalDataString xsi:type="xsd:string" />
<ErrorDescription xsi:type="xsd:string" />
</Item>
</FailedItems>
<UniqueDocumentReferences>
<UniqueDocumentReference>FA40FE</UniqueDocumentReference>
<UniqueDocumentReference>U55922</UniqueDocumentReference>
</UniqueDocumentReferences>
</ImportIndexDocumentResponse>
And BizTalk / Visual Studio 2005 generates ...
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://www.aniteps.com/xml/schemas/awm/images4">
<xs:element name="ImportIndexDocumentResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="HasErrors" type="xs:boolean" />
<xs:element name="NumberOfErrors" type="xs:unsignedByte" />
<xs:element name="ErrorDescription" type="xs:string" />
<xs:element name="ErrorNumber" type="xs:unsignedByte" />
<xs:element name="FailedItems">
<xs:complexType>
<xs:sequence>
<xs:element name="Item">
<xs:complexType>
<xs:sequence>
<xs:element name="OriginalDataString" />
<xs:element name="ErrorDescription" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="UniqueDocumentReferences">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="UniqueDocumentReference" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xsd:schema>
The XSD generator is reasonably dumb, and didn't notice that the sample instance document contained type annotations (xsi:type attributes). So it decided that <ErrorDescription> should contain unsigned bytes and not signed integers, as the type annotation said.
What happens then is that the validator becomes confused because there are two distinct type definitions for the element, so who should it believe? You could try changing the schema so that the type definitions match the xsi:type overrides, that may help a bit.

Resources