从firebase中获取Json作为字符串在java中不起作用

时间:2016-08-23 10:03:07

标签: java json jackson firebase-realtime-database apache-httpclient-4.x

使用休息的Firebase数据: enter image description here

控制台中的FireBase数据: enter image description here 用于从firebase获取数据的Java代码

SELECT count(*) as count FROM gameresnum gone
JOIN gameresnum gtwo ON gone.DRAWNO=gtwo.DRAWNO WHERE gone.RESNUM=1 AND gtwo.RESNUM=2

java中的错误:

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

为什么它无法将字符串解析为对象?

修改 SolutionDto.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]

从firebase导出Json:

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

下面的代码可以正常使用上面的解决方案Dto,但是当使用来自firebase的apache获取它时它不起作用

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

1 个答案:

答案 0 :(得分:2)

正如您的例外所说.... 没有单字符串构造函数/工厂方法

您需要提供单字符串构造函数,即:

@SP  // loads the A register with the value SP (SP predefined to 0)
A=M  // loads the A register with the contents of MEM[0] (the stack pointer)
D=M  // loads D with the contents of MEM[Stack]

或按照here

所述创建由public SolutionDto(String data) 注释的工厂