Can not deserialize instance of java.util.HashMap out of START_ARRAY token

Sun, Jul 26, 2020

Read in 4 minutes

Introduction :

Table of content :

1. Introduction to REST API
2. Create a spring boot project
3. POM.xml dependencies added 
4. Rest controller and other annotation used
5. Possible Errors and Exceptions
6. Conclusion 

Introduction to REST API :

Everyone knows that REST getting more popular nowadays and all the companies started building the REST API’s for their business layer. In this blog we will develop spring boot REST service which will connect with the third party REST API to get the data.

For the Third party REST API, I use this rest endpoint. This endpoint gives you array of JSON data.

Create Spring boot Project :

To create a spring boot project, you can use Spring suite tool or you can install spring boot plugin in your eclipse. For new spring boot project clicck on file -> new -> spring starter project.


If you want more details on how to create spring boot project, you can refer here Springboot Starter

POM.xml dependencies added :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
<relativePath/> <!-- lookup parent from repository -->
<description>Demo project for Spring Boot rest service </description>






From the above dependencies, the spring-boot-starter-web is to develop web application. Here the tomcat server is embedded with spring boot project so, so you run in local and test the service with local host.

Rest Controller and other annotations used :

@RestController- This is the combination of @controller and @responsebody 

@Bean  - explicitly declare a single bean 

@Value - get the value mentioned the file 

@Autowired - is for dependency injection 

@GetMapping. This is the shortcut for  @RequestMapping(method = RequestMethod.GET) and using this annotation we can define endpoint for our rest service. In the below program I have added as @GetMapping("/getDetails") so, the enpoint for my service will be http://localhost:8080/getDetails 
public class ProductRestController {
private static final Logger log = LoggerFactory.getLogger(ProductRestController.class);
String endpoint;
RestTemplate restTemplate;
public RestTemplate restTemplate(RestTemplateBuilder builder) {
public List<HashMap<String, Object>> getDetails()
throws URISyntaxException, JsonMappingException, JsonProcessingException {
HttpHeaders headers = new HttpHeaders();
headers.add("user-agent", "Application");
HttpEntity<String> entity = new HttpEntity<>(headers);"endpoint " + endpoint);
ResponseEntity<String> responseEntity =, HttpMethod.GET, entity, String.class);
String data = responseEntity.getBody();
System.out.println("data ==  " + data);
ObjectMapper mapper = new ObjectMapper();
List<HashMap<String, Object>> dataAsMap = mapper.readValue(data, List.class);
return dataAsMap;


Code Explanation:

To consume the rest api, here were are using “, HttpMethod.GET, entity, String.class)” and we are getting the resposne boody as string. Now have to convert the resposne body to “HashMap” to return resposne as key value pair as JSON response. To convert the string to HashMap we are using ObjectMapper. This piece of code mapper.readValue(data, List.class) will convert the string to a HashMap type.

And the respose data what I am receiving form the endpoint “" is array of objects and one Hasmmap object is not enough. So I am declaring as List<HashMap<String, Object» as a type of dataAsMap and also returning the same.So the return type of this method also List<HashMap<String, Object».

Possible Errors and Exceptions:

As mentioned in the “code explanation” , we are receiving array of objects from the endpoint and to handle that we should convert that resposne as list of HashMap.If you are using HashMap<String, Object> as type of the “dataAsMap” instead of List<HashMap<String, Object> you will get the below error.

Can not deserialize instance of java.util.HashMap out of START_ARRAY token.

Conclusion :

In this article, I am just writing all the business logic in the controller itself. But as per the standard, you have to separate the business logic as service class and you can call the service class method in controller class.