I'm trying to use the following format for making a put request through the RESTtemplate.
@Autowired
RestTemplate template;
@RequestMapping(value = "/change", method = RequestMethod.PUT)
public ModelAndView change(Data data){
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_XML);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
HttpEntity<Data> entity = new HttpEntity<Data>(data, headers);
String url="http://www...com";
try {
template.put(url, entity);
} catch (Exception e) {
System.out.println(e);
}
return new ModelAndView("redirect:/home");
}
I checked on the database and I realized that there is no change. Even the request is not written on the log file. When I'm debugging, I am not getting any error. Probably I'm not using correctly the put method. Can anyone suggest me how should I use the put method or what else should I try to perform a put request with the RestTemplate?
Also i try to use the exchange method instead of the put:
try {
ResponseEntity<Data> result = template.exchange(Url, HttpMethod.PUT, entity, Data.class);
} catch (Exception e) {
System.out.println(e);
}
But in this case i'm taking the following exception:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [website.Data] and content type [text/html;charset=utf-8].
As you can see from the headers i set the content type as application/xml and not text/html. I look at the headers and i can see that contained:
Accept: application/xml
I really can't understand. What else should i change? Any comments on the exception?
Configuration:
<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"
p:basename="config/views" p:order="1" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/classes/config/xml-views.xml</value>
</property>
<property name="order" value="0" />
</bean>
<!--It is used for redirect-->
<bean id="urlBasedViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value=""/>
<property name="suffix" value=""/>
<property name="order" value="2" />
</bean>
<context:annotation-config />
<!--<context:annotation-config />-->
<context:component-scan base-package="data.controller" />
<context:component-scan base-package="data.service" />
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
</list>
</property>
</bean>