Sameer Malhotra
Sameer Malhotra

Reputation: 464

Can not deserialize instance of java.lang.String out of START_OBJECT using Spring Rest Template

I am using the Rest Template to invoke a third party Restful API. but I am getting the "Can not serialize instance of java.lang.String out of START_OBJECT" Below is the code:

xml file:

<!-- RESTful interaction -->
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <ref bean="jsonConverter" />
        </list>
    </property>
</bean>

**java code:**

response= restTemplate.getForObject(url, String.class);

The url is valid URL in the restTemplate.getForObject() method. I verified it putting the same URL in the browser and it produces the valid JSON object.

Below is the error I am getting:

[5/9/13 16:25:56:352 CDT] 00000028 SystemErr R org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@3dd83dd8; line: 1, column: 1]; nested exception is org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@3dd83dd8; line: 1, column: 1]

What am I doing Wrong?

Upvotes: 2

Views: 10172

Answers (1)

user984175
user984175

Reputation: 80

I found the solution. The problem was that I was only using one message converter. After removing the

<property name="messageConverters">
        <list>
            <ref bean="jsonConverter" />
        </list>
    </property>

This code from the bean declaration it started working. Because now RestTemplate started using it's own message converters. Now the code looks like this:

<!-- RESTful interaction -->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate" />

Upvotes: 4

Related Questions