私はいくつかのAPIサービス呼び出しの統合を書いています。これらのサービスの1つは簡単でした。速いItem item = restTemplate.getForObject(url, Item.class)
ここで、Item
は単なるPOJOであり、すべてがそのオブジェクトのすぐ右にマップされています。Spring RestTemplate JSON応答にXML固有の例外を与えるGetForObject
他のサービスには問題があります。呼び出しはほとんど同じです:Attribute[] attribute = restTemplate.getForObject(url, Attribute[].class)
JSONレスポンスにトップレベルの配列があるという違いがあります。ここAttribute
クラスです:
@Data public class ItemAttributes {
private Long id;
private Attribute[] attributes;
@Data
private static class Attribute {
private String name;
//a bunch of other fields
}
}
すべての私の掘削は、上記の動作するはずと言います。代わりに、私はこれを得る:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read
document: javax.xml.stream.XMLStreamException:
ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.; nested exception is java.io.IOException: javax.xml.stream.XMLStreamException:
ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:224)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:208)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:599)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:264)
at com.homedepot.order.gateway.resource.ItemAttributeServiceResource.getItemAttributes(ItemAttributeServiceResource.java:37)
at com.homedepot.order.gateway.resource.ItemAttributeServiceResource.getItemAttributes(ItemAttributeServiceResource.java:32)
at com.homedepot.order.gateway.resource.ItemAttributeServiceResourceTest.testStuffOneItem(ItemAttributeServiceResourceTest.java:62)
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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:821)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:773)
at org.testng.TestRunner.run(TestRunner.java:623)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
at org.testng.TestNG.run(TestNG.java:1018)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:122)
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: java.io.IOException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
at com.fasterxml.jackson.dataformat.xml.util.StaxUtil.throwXmlAsIOException(StaxUtil.java:24)
at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:539)
at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:29)
at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:802)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2810)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:221)
... 36 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:596)
at com.fasterxml.jackson.dataformat.xml.XmlFactory._initializeXmlReader(XmlFactory.java:648)
at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:537)
... 40 more
そしてさらに掘り起こすだけで、私を円に導いている。まず、それはXMLの特定の例外です。私が二重チェックしたことは、実際にはJSON応答です。第二に、が XMLであっても、RestTemplateは正しいHttpMessageConverterとマッパーを自動的に登録する必要があります。 (?右のそれはドキュメントが言うものだ。)
私はResponseEntity<String> response = restTemplate.getForEntity(url, String.class)
と印刷response.getBody().toString()
を使用している場合、私はこのようなものを得る:私はそれを実行することができます知っている
[{"id":12345678,"attributes":[{"f1":"value1","f2":"value2","f3":"value3"},
{"f1":"value4","f2":"value5","f3":"value6"}]}]
を、ObjectMapper
およびパーサの作成します私が望むものをつかんでください。しかし、それはの大きな問題に対処していません。なぜなら、という別のアプローチがうまくいかず、なぜその例外がスローされているのでしょうか。
ご協力いただきますようお願い申し上げます。