mardi 31 mars 2015

Cannot convert value of type [org.springframework.web.client.RestTemplate] to required type [org.springframework.web.client.RestTemplate]

I am trying to use RestTemplate and getting below error only when I am trying to initialize the RestTemplate bean.



Cannot convert value of type [org.springframework.web.client.RestTemplate] to required type [org.springframework.web.client.RestTemplate] for property 'restTemplate': no matching editors or conversion strategy found



This is my bean configuration : Here i am initializing the restTemplate to use it in MyService.java class



<bean id="myService" class="com.test.MyService">
<property name="serviceUrl" value="http://ift.tt/1OVNsTI"/>
<property name="restTemplate" ref="restTemplate"/>
</bean>

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
</bean>


This is my java code



package com.test;
import org.springframework.web.client.RestTemplate;

public class MyService {

private String serviceUrl;
private RestTemplate restTemplate;

public String invoke(){
//restTemplate = new RestTemplate(); -- This works, if bean //initialization removed from spring xml file
String result = restTemplate.getForObject(serviceUrl, String.class);
return result;
}

//@Required
public void setServiceUrl(String serviceUrl) {
this.serviceUrl = serviceUrl;
}

public String getServiceUrl() {
return this.serviceUrl;
}

public RestTemplate getRestTemplate() {
return restTemplate;
}

public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
}


But same code is working if I create object of RestTemplate in java class itself and remove the



<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>


from my spring xml file.


Aucun commentaire:

Enregistrer un commentaire