1

Firebase data using rest: enter image description here

FireBase data in console: enter image description here Java code to fetch data from firebase

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(100);
        cm.setDefaultMaxPerRoute(100);
        httpClient =  HttpClients.custom()
        .setConnectionManager(cm)               
        .build();
        CloseableHttpResponse response;
        try {
            String url = "https://testcustom-a1a4d.firebaseio.com/1719126/1719130/1719121.json?auth=myauthId";
            HttpGet httpGet = new HttpGet(url);         
            response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            SolutionDto solutionDto = objectMapper.readValue(entity.getContent(), SolutionDto.class);
            System.out.println(solutionDto);

        } catch (IOException e) {
            log.error("something went wrong while processing requrest to fireBase", e);         
        }

Error in java:

    com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.web.dtos.SolutionDto] from String value ('{"testId":"1719126"}'); no single-String constructor/factory method
 at [Source: org.apache.http.conn.EofSensorInputStream@554083; line: 1, column: 1]
        at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148) ~[jackson-databind-2.4.3.jar:2.4.3]
        at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:757) ~[jackson-databind-2.4.3.jar:2.4.3]
        at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:277) ~[jackson-databind-2.4.3.jar:2.4.3]
        at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:289) ~[jackson-databind-2.4.3.jar:2.4.3]
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1133) ~[jackson-databind-2.4.3.jar:2.4.3]
        at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:135) ~[jackson-databind-2.4.3.jar:2.4.3]
        at com.fasterxml.jackson.module.afterburner.deser.SuperSonicBeanDeserializer.deserialize(SuperSonicBeanDeserializer.java:123) ~[jackson-module-afterburner-2.4.3.jar:2.4.3]
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051) ~[jackson-databind-2.4.3.jar:2.4.3]
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2192) ~[jackson-databind-2.4.3.jar:2.4.3]

Why it is not able to parse string to object?

EDIT: SolutionDto.java

@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class SolutionDto {
    private Long testId;
    public SolutionDto(){}
    public SolutionDto(Long testId) {
       this.testId = testId;
    }
    public void setTestId(Long testId) {
        this.testId = testId;
    }
    public Long getTestId() {
        return testId;
    }
}

Export of Json from firebase:

{
  "1719126" : {
    "1719130" : {
      "1719121" : "{\"testId\":\"1719126\"}"
    }
  }
}

The below code work fine with above solutionDto, but when the same is fetch using apache from firebase it does not work

String s = "{\"testId\":\"1719126\"}";
objectMapper.readValue(s, SolutionDto.class);
Bhuvan
  • 4,028
  • 6
  • 42
  • 84
  • 1
    Aside from Jordi's answer, there is not much more we can say without seeing `SolutionDto`. Also note that you've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export button in your Firebase Database console. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data, use it in our answer, and in general is just a Good Thing to do. – Frank van Puffelen Aug 23 '16 at 13:33
  • @FrankvanPuffelen added missing details – Bhuvan Aug 23 '16 at 14:53

1 Answers1

2

As your exception says.... no single-String constructor/factory method

You need to provide a single-String constructor i.e:

public SolutionDto(String data)

Or create a factory annotated by @JsonCreator as described here

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • request to look at the edit section... also the json printed in the exception looks ok to me `...from String value ('{"testId":"1719126"}'); no single-Str...` – Bhuvan Aug 23 '16 at 15:17