我正在使用Spring的RestTemplate来调用一个返回XML的API。我遇到了一些问题,无法确定如何将XML响应映射到POJO。
被调用的XML示例为:
<?xml version="1.0" encoding="utf-8"?>
<ABRPayloadSearchResults xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://abr.business.gov.au/ABRXMLSearch/">
<request>
<identifierSearchRequest>
<authenticationGUID>08914fce-7c0f-4aa1-9b25-d31fbe5bcf78</authenticationGUID>
<identifierType>ABN</identifierType>
<identifierValue>33051775556</identifierValue>
<history>N</history>
</identifierSearchRequest>
</request>
<response>
<usageStatement>The Registrar of the ABR monitors the quality of the information available on this website and updates the information regularly. However, neither the Registrar of the ABR nor the Commonwealth guarantee that the information available through this service (including search results) is accurate, up to date, complete or accept any liability arising from the use of or reliance upon this site.</usageStatement>
<dateRegisterLastUpdated>2016-03-21</dateRegisterLastUpdated>
<dateTimeRetrieved>2016-03-21T09:19:43.8712423+11:00</dateTimeRetrieved>
<businessEntity201408>
<recordLastUpdatedDate>2016-02-05</recordLastUpdatedDate>
<ABN>
<identifierValue>33051775556</identifierValue>
<isCurrentIndicator>Y</isCurrentIndicator>
<replacedFrom>0001-01-01</replacedFrom>
</ABN>
<entityStatus>
<entityStatusCode>Active</entityStatusCode>
<effectiveFrom>1999-11-01</effectiveFrom>
<effectiveTo>0001-01-01</effectiveTo>
</entityStatus>
<ASICNumber>051775556</ASICNumber>
<entityType>
<entityTypeCode>PUB</entityTypeCode>
<entityDescription>Australian Public Company</entityDescription>
</entityType>
<goodsAndServicesTax>
<effectiveFrom>2000-07-01</effectiveFrom>
<effectiveTo>0001-01-01</effectiveTo>
</goodsAndServicesTax>
<mainName>
<organisationName>TELSTRA CORPORATION LIMITED</organisationName>
<effectiveFrom>2000-01-20</effectiveFrom>
</mainName>
<mainTradingName>
<organisationName>TELSTRA</organisationName>
<effectiveFrom>2000-01-20</effectiveFrom>
</mainTradingName>
<otherTradingName>
<organisationName>TELSTRA CORPORATION LIMITED</organisationName>
<effectiveFrom>2000-09-12</effectiveFrom>
</otherTradingName>
<otherTradingName>
<organisationName>TELSTRA CORPORATION LIMITED</organisationName>
<effectiveFrom>2000-07-21</effectiveFrom>
</otherTradingName>
<otherTradingName>
<organisationName>TELSTRA</organisationName>
<effectiveFrom>2000-04-26</effectiveFrom>
</otherTradingName>
<mainBusinessPhysicalAddress>
<stateCode>VIC</stateCode>
<postcode>3000</postcode>
<effectiveFrom>2014-09-18</effectiveFrom>
<effectiveTo>0001-01-01</effectiveTo>
</mainBusinessPhysicalAddress>
<businessName>
<organisationName>Anywhere Healthcare</organisationName>
<effectiveFrom>2015-05-19</effectiveFrom>
</businessName>
<businessName>
<organisationName>Belong Broadband</organisationName>
<effectiveFrom>2015-01-14</effectiveFrom>
</businessName>
<businessName>
<organisationName>KIT STORE</organisationName>
<effectiveFrom>2014-11-05</effectiveFrom>
</businessName>
<businessName>
<organisationName>MY HEALTH GATEWAY</organisationName>
<effectiveFrom>2014-10-23</effectiveFrom>
</businessName>
<businessName>
<organisationName>ReadyCare Australia</organisationName>
<effectiveFrom>2014-08-28</effectiveFrom>
</businessName>
<businessName>
<organisationName>ReadyCare Corporation</organisationName>
<effectiveFrom>2014-08-28</effectiveFrom>
</businessName>
<businessName>
<organisationName>Medgate</organisationName>
<effectiveFrom>2014-08-05</effectiveFrom>
</businessName>
<businessName>
<organisationName>Telstra Medgate</organisationName>
<effectiveFrom>2014-08-05</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA GLOBAL</organisationName>
<effectiveFrom>2012-06-21</effectiveFrom>
</businessName>
<businessName>
<organisationName>AUTOMOTIVE ALLIANCE</organisationName>
<effectiveFrom>2011-12-29</effectiveFrom>
</businessName>
<businessName>
<organisationName>AUTOMOTIVE ALLIANCE</organisationName>
<effectiveFrom>2011-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>AUTOMOTIVE ALLIANCE</organisationName>
<effectiveFrom>2011-10-26</effectiveFrom>
</businessName>
<businessName>
<organisationName>AUTOMOTIVE ALLIANCE</organisationName>
<effectiveFrom>2011-10-24</effectiveFrom>
</businessName>
<businessName>
<organisationName>AUTOMOTIVE ALLIANCE</organisationName>
<effectiveFrom>2011-10-12</effectiveFrom>
</businessName>
<businessName>
<organisationName>AUTOMOTIVE ALLIANCE</organisationName>
<effectiveFrom>2011-10-07</effectiveFrom>
</businessName>
<businessName>
<organisationName>AUTOMOTIVE ALLIANCE</organisationName>
<effectiveFrom>2011-09-05</effectiveFrom>
</businessName>
<businessName>
<organisationName>AUTOMOTIVE ALLIANCE</organisationName>
<effectiveFrom>2011-09-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>T LIFE</organisationName>
<effectiveFrom>2010-03-12</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES SERVICES</organisationName>
<effectiveFrom>2008-07-18</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES SERVICES</organisationName>
<effectiveFrom>2008-07-07</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES SERVICES</organisationName>
<effectiveFrom>2008-06-18</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES SERVICES</organisationName>
<effectiveFrom>2008-06-11</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES SERVICES</organisationName>
<effectiveFrom>2008-06-10</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES</organisationName>
<effectiveFrom>2008-02-12</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES SERVICES</organisationName>
<effectiveFrom>2008-02-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES SERVICES</organisationName>
<effectiveFrom>2008-02-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES</organisationName>
<effectiveFrom>2008-01-24</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES</organisationName>
<effectiveFrom>2008-01-03</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES</organisationName>
<effectiveFrom>2007-12-18</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES</organisationName>
<effectiveFrom>2007-12-12</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES</organisationName>
<effectiveFrom>2007-12-12</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES</organisationName>
<effectiveFrom>2007-12-11</effectiveFrom>
</businessName>
<businessName>
<organisationName>SPECIALISED RECOVERIES</organisationName>
<effectiveFrom>2007-12-10</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA LEARNING ACADEMY</organisationName>
<effectiveFrom>2007-09-06</effectiveFrom>
</businessName>
<businessName>
<organisationName>TRADING POST</organisationName>
<effectiveFrom>2005-03-21</effectiveFrom>
</businessName>
<businessName>
<organisationName>TRADING POST</organisationName>
<effectiveFrom>2005-03-11</effectiveFrom>
</businessName>
<businessName>
<organisationName>NETWORKING TASMANIA</organisationName>
<effectiveFrom>2005-02-28</effectiveFrom>
</businessName>
<businessName>
<organisationName>TRADING POST</organisationName>
<effectiveFrom>2005-01-06</effectiveFrom>
</businessName>
<businessName>
<organisationName>CREDITECH</organisationName>
<effectiveFrom>2004-11-16</effectiveFrom>
</businessName>
<businessName>
<organisationName>TRADING POST</organisationName>
<effectiveFrom>2004-11-15</effectiveFrom>
</businessName>
<businessName>
<organisationName>CREDITECH</organisationName>
<effectiveFrom>2004-10-26</effectiveFrom>
</businessName>
<businessName>
<organisationName>CREDITECH</organisationName>
<effectiveFrom>2004-10-25</effectiveFrom>
</businessName>
<businessName>
<organisationName>CREDITECH</organisationName>
<effectiveFrom>2004-10-21</effectiveFrom>
</businessName>
<businessName>
<organisationName>CREDITECH</organisationName>
<effectiveFrom>2004-10-21</effectiveFrom>
</businessName>
<businessName>
<organisationName>BIGPOND MUSIC</organisationName>
<effectiveFrom>2003-12-05</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA LICENSED SHOP</organisationName>
<effectiveFrom>2003-07-08</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA SHOP</organisationName>
<effectiveFrom>2003-07-08</effectiveFrom>
</businessName>
<businessName>
<organisationName>BIGPOND</organisationName>
<effectiveFrom>2003-07-02</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA LICENSED SHOP</organisationName>
<effectiveFrom>2003-06-18</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELECOM AUSTRALIA</organisationName>
<effectiveFrom>2003-02-21</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA'S KIDS FUND</organisationName>
<effectiveFrom>2002-04-08</effectiveFrom>
</businessName>
<businessName>
<organisationName>THINGS THAT GO</organisationName>
<effectiveFrom>2002-03-04</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA BUSINESS SHOP</organisationName>
<effectiveFrom>2002-01-21</effectiveFrom>
</businessName>
<businessName>
<organisationName>MOBILEFUN</organisationName>
<effectiveFrom>2001-07-04</effectiveFrom>
</businessName>
<businessName>
<organisationName>CONFERLINK</organisationName>
<effectiveFrom>2001-05-21</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRAWHOLESALE</organisationName>
<effectiveFrom>2001-02-07</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA COUNTRY WIDE</organisationName>
<effectiveFrom>2000-06-20</effectiveFrom>
</businessName>
<businessName>
<organisationName>TRADING POST</organisationName>
<effectiveFrom>2000-05-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>CREDITECH</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>CREDITECH</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>CREDITECH</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>PHONEWATCH</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELECOM AUSTRALIA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELECOM AUSTRALIA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELECOM AUSTRALIA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELECOM AUSTRALIA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELECOM AUSTRALIA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELECOM AUSTRALIA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELECOM AUSTRALIA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TELSTRA SHOP</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TRADING POST</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>TRADING POST</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>WHEREIS</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>YELLOW PAGES</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>YELLOW PAGES</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>YELLOW PAGES</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>YELLOW PAGES</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>YELLOW PAGES</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
<businessName>
<organisationName>YELLOW PAGES AUSTRALIA</organisationName>
<effectiveFrom>1999-11-01</effectiveFrom>
</businessName>
</businessEntity201408>
</response>
</ABRPayloadSearchResults>
试图将XML解组为POJO的代码如下:
String queryString = "/abrxmlsearch/ABRXMLSearch.asmx/SearchByABNv201408";
RestTemplate restTemplate = new RestTemplate();
URI targetUrl = UriComponentsBuilder.fromUriString(endpoint)
.path("/abrxmlsearch/ABRXMLSearch.asmx/SearchByABNv201408")
.queryParam("includeHistoricalDetails", "N")
.queryParam("searchString", naturalKey)
.queryParam("authenticationGuid", authenticationGuid)
.build()
.toUri();
//ResponseEntity<String> response = restTemplate.getForEntity(targetUrl, String.class);
AbrPayloadSearchResults response = restTemplate.getForObject(targetUrl, AbrPayloadSearchResults.class);
我目前的POJO是
@XmlRootElement(name="ABRPayloadSearchResults")
public class AbrPayloadSearchResults {
@XmlElement(name = "request", required = true)
protected Object request;
@XmlElement(name = "response", required = true)
protected Object response;
}
在这里我好像遇到了第一个难题,因为服务器反馈出现了根元素的问题。
org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to [class AbrPayloadSearchResults]: unexpected element (uri:"http://abr.business.gov.au/ABRXMLSearch/", local:"ABRPayloadSearchResults"). Expected elements are <{}ABRPayloadSearchResults>; nested exception is javax.xml.bind.UnmarshalException: unexpected element (uri:"http://abr.business.gov.au/ABRXMLSearch/", local:"ABRPayloadSearchResults"). Expected elements are <{}ABRPayloadSearchResults>
at org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter.readFromSource(Jaxb2RootElementHttpMessageConverter.java:143)
at org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.readInternal(AbstractXmlHttpMessageConverter.java:61)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:161)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:104)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:599)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:572)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:280)
at com.pegasus.apollo.hub.idprovider.impl.abr.AbrIdProvider.findByNaturalKey(AbrIdProvider.java:37)
at com.pegasus.apollo.hub.idprovider.impl.abr.AbrIdProviderServiceE2ETest.testFindByNaturalKey(AbrIdProviderServiceE2ETest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://abr.business.gov.au/ABRXMLSearch/", local:"ABRPayloadSearchResults"). Expected elements are <{}ABRPayloadSearchResults>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
有人能帮我解决我在XML注释方面搞砸了什么吗?
谢谢。