如何解决XMLStreamException: xmlns 已绑定到 将其重新绑定到 http://deutsche-boerse.com/DBRegHub 是一个错误
我在 spring-batch
应用程序中使用 spring-boot
。 Spring Boot 版本是 2.3.3.RELEASE
。我无法将 xmlns 移动到根标记。
命名空间和架构信息应附加在根级别
<?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>
我实际得到的回应
<?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>
以下是用于配置 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,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;
}
}
我必须评论 rootElementAttribs.put("xmlns","http://deutsche-boerse.com/DBRegHub");
否则它会抛出
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]
更新:如果我从 TransactionPositionReport 类中删除 @XmlRootElement(name = "transaction"),我开始遇到异常
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]
解决方法
这是因为事务元素被标记为根元素,而它不应该:
@XmlRootElement(name = "transaction")
public class TransactionPositionReport {
//...
}
<transaction/>
标签不是根元素,您只需要将 <reportFile>
标记为根元素,以便 Spring Batch 为其添加命名空间属性。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。