Reputation: 305
I am writing a spring mvc app(Spring newbie) which has to call a rest service. I have the rest service deployed in my VM(weblogic 10.3.6 in Linux) and the app I am writing is in my local laptop weblogic(10.3.6 in Windows 8.1).
When I try calling the rest service the request goes fine to the restservice app, but the response fails with the following message
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.myclass.Acct] and content type [application/json;charset=UTF-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:110)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:572)
I am initializing the rest client in my controller & calling a method as below
Class1 ccc = new Class1();
Client client = new Client("REST URL","key","key1","USR",ccc);
client.getService("String"));
In the actual client, the call to rest service looks like this
Acct acct1 = restClient.getRestTemplate().getForObject("URL", Acct.class, "USR");
The error I get is here in the above line. I am not sure how to set the response type. When I change the Acct.class to String.class and Acct acct1 to Object acct1, then it works.
Do I need to set anything in my dispatcher-servlet.xml for the response type json? Let me know if any other configurations are needed to make this work. I did look at other posts related to this, but it did not help. Thanks.
Upvotes: 1
Views: 9199
Reputation: 305
Thanks for the help. I have solved the issue and it is working now.
I made the following updates:
in my pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.3</version>
</dependency>
in my dispatcher servlet
<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
in my code(Acct class)
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
...........
..........
@JsonIgnore
@JsonProperty(value = "........")
public Set getMethod()
{
return this.............;
}
Now, I am able to get the results from the rest services.
Thanks.
Upvotes: 4