我无法通过rest控制器发送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;
}
请帮助我