首页 > 解决方案 > com.fasterxml.jackson.databind.JsonMappingException 同时发送 Json

问题描述

我无法通过休息控制器发送 JsonObject:无法写入 HTTP 消息:org.springframework.http.converter.HttpMessageNotWritableException:无法写入 JSON:JsonObject;嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException: JsonObject (通过引用链:com.google.gson.JsonObject[0]->com.google.gson.JsonObject["asString"])

private String str = "{ \"document\": [\n" + 
            "      {\n" + 
            "        \"file\": \"PayrollFAQ.pdf\",\n" + 
            "        \"type\": \"pdf\",\n" + 
            "        \"title\": \" FAQ for Payroll\",\n" + 
            "        \"rating\": 4.5,\n" + 
            "        \"confidence\": 0.9\n" + 
            "      }\n" + 
            "    ]}";

    @RequestMapping(value = "/posttest", method = RequestMethod.POST, produces="application/json"  )    
    public @ResponseBody ResponseEntity posttest(@RequestBody Information inputReq)  {
        JsonParser jsonParser = new JsonParser();
        List testArray = new ArrayList();
        JsonObject objectFromString = jsonParser.parse(str).getAsJsonObject();      
        testArray.add(HrBotUtilities.getArrayJSON(objectFromString, "document"));
        System.out.println("testString : "+testArray.get(0));



        return ResponseEntity.status(HttpStatus.OK).body(testArray.get(0));

    }

   // HrBotUtilities.getArrayJSON Method
    public static JsonObject[] getArrayJSON(JsonObject docObj, String name) {
            JsonObject[] list = null;
            if (docObj.has(name)) {
                JsonArray json;

                    json = docObj.getAsJsonArray(name);
                    int lenFeatures = json.size();
                    list = new JsonObject[lenFeatures];
                    for (int j = 0; j < lenFeatures; j++) {                 
                        JsonObject f = json.get(j).getAsJsonObject();
                        list[j] = f;
                    }


            }
            return list;

        }

请帮我

标签: java

解决方案


推荐阅读