RestClientException:无法提取响应:没有为响应类型找到合适的HttpMessageConverter

时间:2015-11-04 07:51:08

标签: spring rest spring-mvc resttemplate spring-restcontroller

我正在尝试通过RestTemplate访问第三方API,但我已经困难了好几天......

@Controller
public class ListUsersController {

  @RequestMapping("/listUsers")
     public ModelAndView listUsers() { 
      System.out.println("list user line 1 ... ");
        RestTemplate restTemplate = new RestTemplate();
        System.out.println("list user line 2a ");

        //-----------------------------  
        HttpHeaders requestHeaders = new HttpHeaders(); // Sending multipart/form-data 
        requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);

        // Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request 
        HttpEntity<Object> requestEntity = new HttpEntity<>(requestHeaders);

        restTemplate.getMessageConverters().add(new FormHttpMessageConverter()); 
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        String url="http://ratings.food.gov.uk/authorities/en-GB/json";    
        System.out.println("list user line 3 ");

        //The next line is failing, I have tried all possible implementation after googling etc
        List<LinkedHashMap> users=  restTemplate.getForObject(url, List.class);

        System.out.println("list user line 2b ");


         return new ModelAndView("listUsers", "users", users);
     }
}

我通过浏览器访问该页面,如下所示:

http://localhost:8080/SpringServiceSample-0.0.1-SNAPSHOT/listUsers

浏览器上的错误消息仍为:

HTTP状态500 - 请求处理失败;嵌套异常是org.springframework.web.client.RestClientException:无法提取响应:没有为响应类型[interface java.util.List]和内容类型[text / html; charset = utf-8]

输入例外报告

消息请求处理失败;嵌套异常是org.springframework.web.client.RestClientException:无法提取响应:没有为响应类型[interface java.util.List]和内容类型[text / html; charset = utf-8]

说明服务器遇到内部错误,导致无法完成此请求。

例外

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.springframework.web.client.RestClientException:无法解压缩响应:没有为响应类型[interface java.util.List]和内容类型[text / html; charset = utf-8]找到合适的HttpMessageConverter     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:943)     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)     javax.servlet.http.HttpServlet.service(HttpServlet.java:622)     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)     javax.servlet.http.HttpServlet.service(HttpServlet.java:729)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 根本原因

org.springframework.web.client.RestClientException:无法提取响应:没有为响应类型[interface java.util.List]和内容类型[text / html; charset = utf-8]找到合适的HttpMessageConverter     org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108)     org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:535)     org.springframework.web.client.RestTemplate.execute(RestTemplate.java:489)     org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:226)     com.programmingfree.springservice.controller.ListUsersController.listUsers(ListUsersController.java:51)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>fsa</groupId>
  <artifactId>SpringServiceSample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <repositories>
  <repository>
    <id>central</id>
    <name>Maven Repository Switchboard</name>
    <layout>default</layout>
    <url>http://repo1.maven.org/maven2</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository>
</repositories>

<dependencies>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.2.2.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.2.2.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.2.2.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.3</version>
    </dependency>
    <dependency>
    <groupId>com.google.collections</groupId>
    <artifactId>google-collections</artifactId>
    <version>1.0-rc2</version>
    </dependency>
    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>1.7.12</version>
    </dependency>   
</dependencies>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

任何帮助都会受到赞赏,我已经包含了实际的API,我正在尝试访问。非常感谢。

0 个答案:

没有答案