У меня есть устаревшее приложение, которое я пытаюсь перенести в мое весеннее загрузочное приложение, и у меня возникают проблемы с пространством имен XML.
Я хочу, чтобы мое приложение весенней загрузки могло принимать XML с пространством имен или без него.Например,
с пространством имен
<?xml version="1.0" encoding="iso-8859-1"?>
<NotificationRequest
xmlns="http://request.xmlbeans.cpc.com/NotificationQuery"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</NotificationRequest>
и без пространства имен
<?xml version="1.0" encoding="iso-8859-1"?>
<NotificationRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</NotificationRequest>
Я создал package-info
таким образом для пространства имен:
@javax.xml.bind.annotation.XmlSchema(namespace =
"http://request.xmlbeans.cpc.com/NotificationQuery", elementFormDefault =
javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.onem.xmlbeans.request;
однако, это примет XML с пространством имен, но НЕ примет XML без пространства имен.
Мой код следующий:
@RequestMapping(method = RequestMethod.POST, consumes =
MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces =
MediaType.APPLICATION_XML_VALUE)
public SMSHistoryResponse getSMSHistory(@RequestParam("xmlreq") String
request ) {
SMSHistoryRequest requestObj = new SMSHistoryRequest();
JAXBContext jaxbContext;
try {
// Convert the incoming String XML into POJO
jaxbContext = JAXBContext.newInstance(SMSHistoryRequest.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(request);
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
false);
XMLStreamReader xsr = xif.createXMLStreamReader(new
StreamSource(reader));
requestObj = (SMSHistoryRequest) unmarshaller.unmarshal(xsr);
} catch(JAXBException e) {
e.printStackTrace();
LOGGER.error("JAXB exception parsing error" + e.getMessage());
} catch(XMLStreamException e) {
LOGGER.error("XML Stream exception " + e.getMessage());
}
return smsBuilder.searchSMSHistory(requestObj);
}
Я получаю следующую ошибку, когда он отменяет маршализацию XML (без пространства имен):
javax.xml.bind.UnmarshalException - with linked exception:
[com.sun.istack.internal.SAXParseException2; lineNumber: 2; columnNumber: 22; unexpected element (uri:"", local:"NotificationRequest"). Expected elements are <{http://request.xmlbeans.cpc.com/NotificationQuery}NotificationRequest>]