jsonSchema2pojo杰克逊deserialisation问题(jsonSchema2poj

2019-10-29 17:39发布

我试图使用jsonSchema2pojo POJO生成库,我成功地这样做也是如此。 但问题我面临的是deserialisation异常当我尝试生成地图由typeReferencing产生rootClass即EsRootDoc(在下面的代码)。

这里,是我测试代码:

JCodeModel codeModel = new JCodeModel();
String classLocation = System.getProperty("user.dir")+ "/src/main/java";
GenerationConfig config = new DefaultGenerationConfig() {
                @Override
                public boolean isGenerateBuilders() { // set config option by overriding method
                    return true;
                }

                @Override
                public SourceType getSourceType() {
                    return SourceType.JSON;
                }
final RuleFactory ruleFactory = new RuleFactory(config,
            new Jackson2Annotator(config), new SchemaStore());

            final SchemaMapper mapperSchema = new SchemaMapper(ruleFactory,new SchemaGenerator());

    mapperSchema.generate(codeModel, "EsRootDoc",           "com.tech.EnrichmentService.EnrichmentController.tempJsonToClass", json);

codeModel.build(new File(classLocation));

ObjectMapper mapper = new ObjectMapper();
try{

     p = mapper.readValue(json,new TypeReference<com.tech.EnrichmentService.EnrichmentController.tempJsonToClass.EsRootDoc>() {});

    }
    catch(Exception e){
    e.printStackTrace();
    }

例外:

  com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
 at [Source: java.io.StringReader@72b070e8; line: 1, column: 15974] (through reference chain: com.tech.EnrichmentService.EnrichmentController.tempJsonToClass.EsRootDoc["svo"]->java.util.ArrayList[1]->com.tech.EnrichmentService.EnrichmentController.tempJsonToClass.Svo["svoMetadata"]->com.tech.EnrichmentService.EnrichmentController.tempJsonToClass.SvoMetadata["event"])

现在,如果我在这里产生类,然后typeReferencing同样产生rootClass,我怎么去不同的JSON到POJO的问题?

Editted(加入JSON):

{
  "coref": [
    {
      "endOffset": 260,
      "coreference": "Hind Times",
      "entity": "them",
      "beginOffset": 256
    }
  ],

  "upCategory": "News",
  "dateFacet": "2016-06-14T04:14:06Z",
  "svo": [
    {
      "clause": "kids go missing from ? forest",
      "svoMetadata": {
        "sentenceID": 1,
        "clauseId": 2,
        "svoId": 0
      },
      "objectPOS": "IN POS NN NN",
      "isRelatedKeySVO": false,
      "subjectPOS": " NNS",
      "subject": "kids",
      "confidence": 0.786255231995661,
      "verb": "missing",
      "verbPOS": "VBG",
      "source": "OpenIE4",
      "isKeySVO": true,
      "object": "from ' forest toilet"
    },
    {
      "clause": "lack of toilets forced them to defacate in the open",
      "svoMetadata": {
        "sentenceID": 1,
        "clauseId": 0,
        "event": [
          "forced"
        ],
        "svoId": 0,
        "verbType": "event,"
      }
    }
  ],
    "projectName": "guest",
  "uniqueId": "575f848ee4b0011ac0e1ef091"
}
文章来源: jsonSchema2pojo jackson deserialisation issue