【发布时间】:2018-02-05 12:54:08
【问题描述】:
我已将响应设置为 JSON,但得到了这个
无法写入 JSON:未找到类 org.json.JSONObject 的序列化程序,也未找到可创建 BeanSerializer 的属性(为避免异常,请禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)
@RequestMapping(value = "/customerlist", method = RequestMethod.POST)
public ResponseGenerator getCustomerList() {
ResponseGenerator responseGenerator = new ResponseGenerator();
try {
responseGenerator.setCode(StatusCode.SUCCESS.code);
responseGenerator.setMessage(StatusCode.SUCCESS.message);
responseGenerator.setStatus(ResponseStatus.SUCCESS);
JSONObject data = userService.getUserList();
responseGenerator.setJSONData(data);
return responseGenerator; //error here
} catch (Exception e) {
logger.error("Error while getting Customer List : ", e);
e.printStackTrace();
responseGenerator.setCode(StatusCode.GENERAL_ERROR.code);
responseGenerator.setMessage(StatusCode.GENERAL_ERROR.message);
responseGenerator.setStatus(ResponseStatus.FAIL);
return responseGenerator;
}
}
userService.getUserList():
public JSONObject jsonResp;
public JSONObject getUserList() throws Exception{
jsonResp =new JSONObject();
//List<JSONObject> customers = new ArrayList<JSONObject>();
JSONObject jsonResponse = erpNextAPIClientService.getCustomerList();
//ObjectMapper objectMapper = new ObjectMapper();
//objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
//JSONArray jsonArray = objectMapper.convertValue(jsonResponse.get("data"), JSONArray.class);
JSONArray jsonArray = jsonResponse.getJSONArray("data");
//JSONArray jsonArray =new Gson().fromJson(jsonResponse.get("data").toString(),JSONArray.class);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject cust = erpNextAPIClientService.getUser(jsonArray.getJSONObject(i).get("name").toString());
JSONObject custAddress =erpNextAPIClientService.getCustomerAddress(jsonArray.getJSONObject(i).get("name").toString());
JSONObject custData = new JSONObject(cust.getString("data"));
JSONObject custAddressData = new JSONObject(custAddress.getString("data"));
custData.accumulate("bill_to_address_line_one",custAddressData.get("address_line1"));
custData.accumulate("bill_to_address_line_two",custAddressData.get("address_line2"));
custData.accumulate("bill_to_city",custAddressData.get("city"));
custData.accumulate("bill_to_state",custAddressData.get("state"));
custData.accumulate("bill_to_zip",custAddressData.get("pincode"));
custData.accumulate("bill_to_country",custAddressData.get("country"));
jsonResp.put("data",custData);
System.out.println(custData.toString());
//customers.add(custData);
}
return jsonResp;
}
【问题讨论】:
-
ResponseGenerator的所有属性都有 getter 吗? -
@LucianovanderVeekens 是的 ResponseGenerator 有 getter setter
-
为什么你使用 org.json,而 spring 已经提供了一个名为 jackson 的全功能解决方案。
-
它会抛出错误,因为 JSONObject 没有暴露它们的默认 getter。