![](/img/trans.png)
[英]Spring RestTemplate getForObject() giving 401 unauthorized exception
[英]Spring RestTemplate GetForObject giving an XML specific exception for a JSON response
我正在为几个API服务调用编写集成。 这些服务之一很简单。 快速的Item item = restTemplate.getForObject(url, Item.class)
,其中Item
只是一个POJO,所有内容都直接映射到该对象中。
其他服务是有问题的。 调用几乎相同: 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
和一个解析器并获取我想要的东西,但这并不能解决更大的问题,即为什么其他方法不起作用以及为什么会抛出该异常。
任何帮助将不胜感激。
两个潜在的问题:1)您确定RestTemplate.getForObject的通用版本可以接受数组类吗? 传递“ Attribute []。class”似乎不正确... 2)返回的JSON不是数组,而是一个映射。 即使将数组类传递给RestTemplate起作用,返回的数据也不会匹配,并且解析将失败。
〜NCM
我的错。 尽管API团队保证响应肯定是“ application / json”,但我应该首先进行尽职调查,并检查响应标头上的Content-Type,因为它设置为“ text / plain”。 一旦他们解决了问题就解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.