I am using spring-batch
in spring-boot
application. The Spring Boot version is 2.3.3.RELEASE
. I am not able to move the xmlns to the root tag.
Namespace and schema info should be attached at root level
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<reportFile xmlns="http://deutsche-boerse.com/DBRegHub" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://deutsche-boerse.com/DBRegHub regulatoryHubUpload_MiFIR_001.60.xsd">
<fileInformation>
<sender>11003220</sender>
<timestamp>2020-12-23T09:05:34Z</timestamp>
<environment>LOCAL</environment>
<version>1.0</version>
</fileInformation>
<record>
<transaction>
</transaction>
<transaction>
</transaction>
<transaction>
</transaction>
</record>
</reportFile>
What I am actually getting in response
<?xml version="1.0" encoding="UTF-8"?>
<reportFile xsi:schemaLocation="http://deutsche-boerse.com/DBRegHub regulatoryHubUpload_MiFIR_001.60.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fileInformation>
<sender>11003220</sender>
<timestamp>2020-12-23T09:05:34Z</timestamp>
<environment>LOCAL</environment>
<version>1.0</version>
</fileInformation>
<record>
<transaction xmlns="http://deutsche-boerse.com/DBRegHub">
</transaction>
<transaction xmlns="http://deutsche-boerse.com/DBRegHub">
</transaction>
<transaction xmlns="http://deutsche-boerse.com/DBRegHub">
</transaction>
</record>
</reportFile>
Below is a code snippet for configuring ItemWriter
public StaxEventItemWriter<TransactionPositionReport> staxTransactionItemWriter() {
Resource exportFileResource = new FileSystemResource(FILE_LOCATION_PATH);
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setSupportJaxbElementClass(true);
marshaller.setClassesToBeBound(TransactionPositionReport.class);
HashMap<String, String> rootElementAttribs = new HashMap<String, String>();
//rootElementAttribs.put("xmlns", "http://deutsche-boerse.com/DBRegHub");
rootElementAttribs.put("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
rootElementAttribs.put("xsi:schemaLocation", "http://deutsche-boerse.com/DBRegHub regulatoryHubUpload_MiFIR_001.60.xsd");
ExtendedStaxEventItemWriter<TransactionPositionReport> writer = new ExtendedStaxEventItemWriter<TransactionPositionReport>();
writer.setName("transactionWriter");
writer.setVersion("1.0");
writer.setResource(exportFileResource);
writer.setMarshaller(marshaller);
writer.setRootTagName("reportFile");
writer.setRootElementAttributes(rootElementAttribs);
writer.setHeaderCallback(omegaXmlHeaderCallBack);
writer.setFooterCallback(getOmegaXmlFooterCallBack());
writer.setShouldDeleteIfEmpty(true);
writer.setIndenting(true);
return writer;
}
import java.io.Writer;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import org.springframework.batch.item.xml.StaxEventItemWriter;
import javanet.staxutils.IndentingXMLEventWriter;
public class ExtendedStaxEventItemWriter<T> extends StaxEventItemWriter<T> {
private boolean indenting;
public void setIndenting(boolean indenting) {
this.indenting = indenting;
}
public boolean isIndenting() {
return indenting;
}
@Override
protected XMLEventWriter createXmlEventWriter(XMLOutputFactory outputFactory, Writer writer)
throws XMLStreamException {
if (isIndenting()) {
return new IndentingXMLEventWriter(super.createXmlEventWriter(outputFactory, writer));
} else {
return super.createXmlEventWriter(outputFactory, writer);
}
}
}
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2021.05.18 at 02:21:55 PM BST
//
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for transactionPositionReport complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="transactionPositionReport">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="processingDetails" type="{http://deutsche-boerse.com/DBRegHub}processingDetails"/>
* <element name="configurableFields" type="{http://deutsche-boerse.com/DBRegHub}configurableFields" minOccurs="0"/>
* <element name="mifir" type="{http://deutsche-boerse.com/DBRegHub}mifirDetails" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "transactionPositionReport", propOrder = {
"processingDetails",
"configurableFields",
"mifir"
})
@XmlRootElement(name = "transaction")
public class TransactionPositionReport {
@XmlElement(required = true)
protected ProcessingDetails processingDetails;
protected ConfigurableFields configurableFields;
protected MifirDetails mifir;
/**
* Gets the value of the processingDetails property.
*
* @return
* possible object is
* {@link ProcessingDetails }
*
*/
public ProcessingDetails getProcessingDetails() {
return processingDetails;
}
/**
* Sets the value of the processingDetails property.
*
* @param value
* allowed object is
* {@link ProcessingDetails }
*
*/
public void setProcessingDetails(ProcessingDetails value) {
this.processingDetails = value;
}
/**
* Gets the value of the configurableFields property.
*
* @return
* possible object is
* {@link ConfigurableFields }
*
*/
public ConfigurableFields getConfigurableFields() {
return configurableFields;
}
/**
* Sets the value of the configurableFields property.
*
* @param value
* allowed object is
* {@link ConfigurableFields }
*
*/
public void setConfigurableFields(ConfigurableFields value) {
this.configurableFields = value;
}
/**
* Gets the value of the mifir property.
*
* @return
* possible object is
* {@link MifirDetails }
*
*/
public MifirDetails getMifir() {
return mifir;
}
/**
* Sets the value of the mifir property.
*
* @param value
* allowed object is
* {@link MifirDetails }
*
*/
public void setMifir(MifirDetails value) {
this.mifir = value;
}
}
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2021.05.18 at 02:21:55 PM BST
//
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for record complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="record">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <choice>
* <element name="transaction" type="{http://deutsche-boerse.com/DBRegHub}transactionPositionReport" maxOccurs="unbounded" minOccurs="0"/>
* <element name="referencePartyDetails" type="{http://deutsche-boerse.com/DBRegHub}referencePartyDetails" maxOccurs="unbounded" minOccurs="0"/>
* </choice>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "record", propOrder = {
"transaction",
"referencePartyDetails"
})
public class Record {
protected List<TransactionPositionReport> transaction;
protected List<ReferencePartyDetails> referencePartyDetails;
/**
* Gets the value of the transaction property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the transaction property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getTransaction().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link TransactionPositionReport }
*
*
*/
public List<TransactionPositionReport> getTransaction() {
if (transaction == null) {
transaction = new ArrayList<TransactionPositionReport>();
}
return this.transaction;
}
/**
* Gets the value of the referencePartyDetails property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the referencePartyDetails property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getReferencePartyDetails().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link ReferencePartyDetails }
*
*
*/
public List<ReferencePartyDetails> getReferencePartyDetails() {
if (referencePartyDetails == null) {
referencePartyDetails = new ArrayList<ReferencePartyDetails>();
}
return this.referencePartyDetails;
}
}
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="fileInformation" type="{http://deutsche-boerse.com/DBRegHub}fileInformation"/>
* <element name="record" type="{http://deutsche-boerse.com/DBRegHub}record"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"fileInformation",
"record"
})
@XmlRootElement(name = "reportFile")
public class ReportFile {
@XmlElement(required = true)
protected FileInformation fileInformation;
@XmlElement(required = true)
protected Record record;
/**
* Gets the value of the fileInformation property.
*
* @return
* possible object is
* {@link FileInformation }
*
*/
public FileInformation getFileInformation() {
return fileInformation;
}
/**
* Sets the value of the fileInformation property.
*
* @param value
* allowed object is
* {@link FileInformation }
*
*/
public void setFileInformation(FileInformation value) {
this.fileInformation = value;
}
/**
* Gets the value of the record property.
*
* @return
* possible object is
* {@link Record }
*
*/
public Record getRecord() {
return record;
}
/**
* Sets the value of the record property.
*
* @param value
* allowed object is
* {@link Record }
*
*/
public void setRecord(Record value) {
this.record = value;
}
}
I have to comment rootElementAttribs.put("xmlns", "http://deutsche-boerse.com/DBRegHub");
otherwise it throws
Caused by: javax.xml.stream.XMLStreamException: xmlns has been already bound to . Rebinding it to http://deutsche-boerse.com/DBRegHub is an error
at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeDefaultNamespace(XMLStreamWriterImpl.java:786) ~[?:1.8.0_291]
at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeNamespace(XMLStreamWriterImpl.java:1003) ~[?:1.8.0_291]
at com.sun.xml.internal.stream.writers.XMLEventWriterImpl.add(XMLEventWriterImpl.java:127) ~[?:1.8.0_291]
at javanet.staxutils.IndentingXMLEventWriter.add(IndentingXMLEventWriter.java:382) ~[stax-utils-20040917.jar:?]
at org.springframework.batch.item.xml.StaxEventItemWriter.startDocument(StaxEventItemWriter.java:632) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.item.xml.StaxEventItemWriter.open(StaxEventItemWriter.java:489) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE]
UPDATE: If I remove @XmlRootElement(name = "transaction") from TransactionPositionReport class, I start getting below exception
2021-06-15 10:10:59,390 ERROR o.s.b.c.s.AbstractStep [taskExecutor-1] Encountered an error executing step extractAndReplacePersonalDataStep in job extractAndReplacePersonalDataJob
org.springframework.batch.core.step.skip.NonSkippableReadException: Non-skippable exception during read
at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:105) ~[spring-batch-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:126) ~[spring-batch-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:118) ~[spring-batch-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:71) ~[spring-batch-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407) ~[spring-batch-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:331) ~[spring-batch-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:273) ~[spring-batch-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82) ~[spring-batch-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 72 more
Caused by: org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException
- with linked exception:
[com.sun.istack.internal.SAXParseException2; lineNumber: 10; columnNumber: 22; unexpected element (uri:"http://deutsche-boerse.com/DBRegHub", local:"transaction"). Expected elements are (none)]
at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:951) ~[spring-oxm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:818) ~[spring-oxm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:788) ~[spring-oxm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.batch.item.xml.StaxEventItemReader.doRead(StaxEventItemReader.java:257) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:93) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_291]
... 72 more
Caused by: javax.xml.bind.UnmarshalException
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:468) ~[?:1.8.0_291]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:448) ~[?:1.8.0_291]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:420) ~[?:1.8.0_291]
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshalStaxSource(Jaxb2Marshaller.java:852) ~[spring-oxm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:801) ~[spring-oxm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:788) ~[spring-oxm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.batch.item.xml.StaxEventItemReader.doRead(StaxEventItemReader.java:257) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:93) ~[spring-batch-infrastructure-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 72 more
Caused by: com.sun.istack.internal.SAXParseException2: unexpected element (uri:"http://deutsche-boerse.com/DBRegHub", local:"transaction"). Expected elements are (none)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726) ~[?:1.8.0_291]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247) ~[?:1.8.0_291]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242) ~[?:1.8.0_291]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109) ~[?:1.8.0_291]
at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:87) ~[spring-batch-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 72 more
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://deutsche-boerse.com/DBRegHub", local:"transaction"). Expected elements are (none)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726) ~[?:1.8.0_291]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247) ~[?:1.8.0_291]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242) ~[?:1.8.0_291]
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109) ~[?:1.8.0_291]
at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:99) ~[spring-batch-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:87) ~[spring-batch-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]