Java Spring RestTemplate GetForObject为JSON响应提供特定于XML的异常

Java Spring RestTemplate GetForObject为JSON响应提供特定于XML的异常,java,json,xml,spring,Java,Json,Xml,Spring,我正在为几个API服务调用编写集成;其中一项服务很简单。一个快速的Item Item=restTemplate.getForObject(url,Item.class)其中Item只是一个POJO,所有内容都映射到该对象中 另一项服务有问题。调用几乎相同:Attribute[]Attribute=restemplate.getForObject(url,Attribute[].class),区别在于JSON响应具有顶级数组。下面是属性类: @Data public class ItemAttri

我正在为几个API服务调用编写集成;其中一项服务很简单。一个快速的
Item Item=restTemplate.getForObject(url,Item.class)
其中
Item
只是一个POJO,所有内容都映射到该对象中

另一项服务有问题。调用几乎相同:
Attribute[]Attribute=restemplate.getForObject(url,Attribute[].class)
,区别在于JSON响应具有顶级数组。下面是
属性
类:

@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 response=restemplate.getForEntity(url,String.class)
并打印
response.getBody().toString()
我会得到如下结果:

[{"id":12345678,"attributes":[{"f1":"value1","f2":"value2","f3":"value3"},
{"f1":"value4","f2":"value5","f3":"value6"}]}]
我知道我可以用它运行,创建一个
ObjectMapper
和一个解析器,然后获取我想要的,但这并不能解决为什么另一种方法不起作用,以及为什么它抛出那个异常的更大问题

任何帮助都将不胜感激。

两个潜在问题: 1) 您确定RestTemplate.getForObject的通用版本可以接受数组类吗?传递“Attribute[].class”似乎不正确。。。 2) 返回的JSON不是数组,而是映射。即使将数组类传递给RestTemplate有效,返回的数据也不会匹配,解析也会失败


~NCM

我错了。尽管API团队保证响应肯定是“application/json”,但我应该先进行尽职调查,并检查响应标题上的内容类型,因为它被设置为“text/plain”。一旦他们修好了,问题就解决了