Announcement Announcement Module
Collapse
No announcement yet.
Retrieving JSON data via HTTP GET using Spring Android Framework Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Retrieving JSON data via HTTP GET using Spring Android Framework

    I am using spring android framework for retrieving json data via Http GET. I am getting following exception for the same :

    - Could not read JSON: Can not deserialize instance of com.springandroidjsondemo.beans.LoginBean[] out of START_OBJECT token

    The bean (LoginBean) is Following

    Code:
    package com.springandroidjsondemo.beans;
    
    public class LoginBean {
    
        private String status;
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    }
    The android code is following :
    Code:
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
    
    HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate();
    // Add the Jackson message converters
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    
    // Make the HTTP GET request, marshaling the response from JSON to an array of Events
    ResponseEntity<LoginBean[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity,LoginBean[].class); // getting exception here
    
    LoginBean[] loginBean = responseEntity.getBody();
    String status = loginBean[0].getStatus();
    The json response from the server is following :
    Code:
    {"emp-data":[{"status":"true"}]}
    I am not sure if any annotations are required for Jackson Marshalling

    Please suggest the solution

    Thanks!

  • #2
    Hi, this is a Jackson deserialization issue. if you compare the JSON response with your LoginBean, the array of statuses is contained within the element "emp-data". However, your RestTemplate request is expecting JSON which looks like the following.

    Code:
    [{"status":"true"},{"status":"false"}]
    You have a couple options. You can create a wrapper object around LoginBean. Or you can try annotating the LoginBean like the following:

    Code:
    @JsonRootName(value = "emp-data")
    public class LoginBean {
    
        private String status;
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    }
    In order for this to work, you probably need to configure the Jackson ObjactMapper.

    Code:
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
    MappingJackson2HttpMessageConverter jackson = new MappingJackson2HttpMessageConverter();
    jackson.setObjectMapper(objectMapper);
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(jackson);

    Comment


    • #3
      Thanks for the solution!!

      Comment

      Working...
      X