0

I am trying to access a third party API, via a RestTemplate but I have been having difficulty for days ...

@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);
     }
}

I am accessing the page via the browser like this:

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

The error message on the browser remains:

HTTP Status 500 - Request processing failed; nested exception is org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.List] and content type [text/html;charset=utf-8]

type Exception report

message Request processing failed; nested exception is org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.List] and content type [text/html;charset=utf-8]

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.List] and content type [text/html;charset=utf-8] 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) root cause

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.List] and content type [text/html;charset=utf-8] 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>

Any help will be appreciate, I have included the actual API, I am trying to access. Thanks a lot.

user2722614
  • 21
  • 1
  • 7

0 Answers0