Java 为什么jsonParser.getCodec().readTree(jsonParser.asText()为我返回一个空字符串?

Java 为什么jsonParser.getCodec().readTree(jsonParser.asText()为我返回一个空字符串?,java,json,spring,rest,post,Java,Json,Spring,Rest,Post,我编写了一个REST服务来接收post请求中的元数据。我使用的是spring data elasticsearch,我创建了一个自定义元数据对象,将Json反序列化为如下所示: @Document(indexName = "metadata_v1", type = "metadata") public class Metadata { @Id private String id; @Field(type = FieldType.String) privat

我编写了一个REST服务来接收post请求中的元数据。我使用的是spring data elasticsearch,我创建了一个自定义元数据对象,将Json反序列化为如下所示:

@Document(indexName = "metadata_v1", type = "metadata")
    public class Metadata {
    @Id
    private String id;
    @Field(type = FieldType.String)
    private String uuid;
    @Field(type = FieldType.String)
    private String userId;
    @Field(type = FieldType.Date, format = DateFormat.basic_date_time)
    private Date date = null;
    @Field(type = FieldType.String)
    private String classification;
    @Field(type = FieldType.Nested)
    private List<NumericKeyValue> numericKeyValue;
    @Field(type = FieldType.Nested)
    private List<TextKeyValue> textKeyValue;
public class NumericKeyValueJsonDeserializer extends JsonDeserializer<List<NumericKeyValue>>{

    @Override
    public List<NumericKeyValue> deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        TypeReference<List<NumericKeyValue>> typeRef = new TypeReference<List<NumericKeyValue>>(){};
        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = jp.getCodec().readTree(jp);
        String numericKeyValue = root.get("numericKeyValue").asText();
        return mapper.readValue( numericKeyValue, typeRef);
    }

}
"numericKeyValue" : {
    "type" : "nested",
    "properties" : {
        "key" : {"type" : "string"},
        "value" : {"type" : "double"}
    }
}
我补充说

@JsonDeserialize(using = NumericKeyValueJsonDeserializer.class)
到我的元数据类中的字段声明

然而,经过大量测试,我逐渐意识到JsonNode
不仅不包含
“numericKeyValue”
,而且在调用
根.asText()时,它会给我一个完全空的字符串

我一直在使用Postman向我的端点发送post请求

@RequestMapping(value="/metadata_v1/ingest", method=RequestMethod.POST, consumes="application/json")
public @ResponseBody Metadata createEntry(@RequestBody Metadata entry){
    repository.save(entry);
    return entry;
}
包含以下Json:

{
    "numericKeyValue": 
    [
      {
        "key": "velocity",
        "value": 55.5
      },
      {
        "key": "angle",
        "value": 90
      }
    ]
}
我的映射如下所示:

@Document(indexName = "metadata_v1", type = "metadata")
    public class Metadata {
    @Id
    private String id;
    @Field(type = FieldType.String)
    private String uuid;
    @Field(type = FieldType.String)
    private String userId;
    @Field(type = FieldType.Date, format = DateFormat.basic_date_time)
    private Date date = null;
    @Field(type = FieldType.String)
    private String classification;
    @Field(type = FieldType.Nested)
    private List<NumericKeyValue> numericKeyValue;
    @Field(type = FieldType.Nested)
    private List<TextKeyValue> textKeyValue;
public class NumericKeyValueJsonDeserializer extends JsonDeserializer<List<NumericKeyValue>>{

    @Override
    public List<NumericKeyValue> deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        TypeReference<List<NumericKeyValue>> typeRef = new TypeReference<List<NumericKeyValue>>(){};
        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = jp.getCodec().readTree(jp);
        String numericKeyValue = root.get("numericKeyValue").asText();
        return mapper.readValue( numericKeyValue, typeRef);
    }

}
"numericKeyValue" : {
    "type" : "nested",
    "properties" : {
        "key" : {"type" : "string"},
        "value" : {"type" : "double"}
    }
}
如果需要,我可以展示更多的东西。我想如果我能以某种方式获得用Java发送的JSON,也许是作为字符串,我会很好。我一直在获取导致空指针异常的空字符串,当我尝试
String numericKeyValue=jp.getText()
时,该字符串只是“[”的当前标记,我想它至少不是空字符串,但仍然对我没有帮助


非常感谢您的帮助或建议。

在您的情况下,
numericKeyValue
的值是一个数组。您应该替换以下行:

String numericKeyValue = root.get("numericKeyValue").asText();
与: