RestTemplate getForObject with无法读取JSON:意外字符('<'(代码60))

时间:2013-06-20 06:39:30

标签: spring resttemplate

我想从http响应读取String,就像这样。 我正在尝试这样做[见下文],它正在抛出错误。

1.Client

String string=getForObject("http://127.0.0.1:6060/dc-server/rest/dataset/Book/meta", String.class);

2.Server

@ResponseBody
@RequestMapping(value="/dataset/{datasetName}/meta", method=RequestMethod.GET)
public String getDatasetMeta(@PathVariable("datasetName") String datasetName) {
    return service.getDatasetMeta(datasetName);
}

spring mvc configure file

<bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
</bean>
<bean id="stringHttpMessageConverter"  class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter" />
            <ref bean="stringHttpMessageConverter"/>
        </list>
    </property>
</bean>

异常

    org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@19a6fa1; line: 1, column: 2]; nested exception is org.codehaus.jackson.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@19a6fa1; line: 1, column: 2]
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:127)
    at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:153)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:81)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:446)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.cgs.dc.client.rest.RestConnector.getDatasetMeta(RestConnector.java:55)
    at server/rest/dataset/Book/meta" resulted in 200 (OK)
14:01:05,395 DEBUG RestTemplate:78 - Reading [java.lang.String] as "application/json" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@1371ddd]

回应机构

<dataset xmlns="http://sucsoft.com/DC_DEF" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ......

1 个答案:

答案 0 :(得分:2)

您正在访问的服务是返回xml响应,而不是json响应。您可能应该做的是使用JAX-B将该xml转换为真正的java对象。

使用this eclipse plugin,您可以从xml文档生成已注释的java类。然后,您可以在restTemplate中使用该类(它不需要任何特殊的spring xml配置)作为您希望xml反序列化的类。

像这样:

Response response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(null), Response.class);

确保Response是带注释的类。

如果你只想要一个字符串,那么请确保spring没有尝试使用json消息转换器对其进行反序列化。