5

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>
4

1 回答 1

3

我猜你的应用程序的客户端是一个网页(然后是 HTML)。这篇文章解释了如何让你的 webapp 为未来的浏览器兼容性做好准备(如果他们稍后决定支持例如 PUT、DELETE 操作)。

总之,对于我们的项目,我们刚刚在 web.xml 中声明了这些行:

<filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/app/*</url-pattern>
</filter-mapping>

文章讨论了要添加的 javascript(可能是 Spring 3.0 M1 之前的版本),但我们发现这个解决方案更好,因为它只是一个配置。

于 2012-09-27T22:26:21.237 回答