Fetching Json from firebase as string not working in java

给你一囗甜甜゛ 提交于 2020-01-05 04:24:07

问题


Firebase data using rest:

FireBase data in console: 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);

回答1:


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



来源:https://stackoverflow.com/questions/39098101/fetching-json-from-firebase-as-string-not-working-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!