无法写入JSON:找不到类org.JSON.JSONObject的序列化程序,也找不到创建BeanSerializer的



我已经将响应设置为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;
}

这将引发一个错误,因为JSONObject不会公开默认的getter。尽管可以采取变通办法来避免这种情况。

您需要更改ResponseGenerator类以接受Map<String, Object>而不是JSONObject。现在更改这一行:

responseGenerator.setJSONData(data);

到此:

responseGenerator.setJSONData(data.toMap());

我希望这能奏效。

附言:我的建议是删除JSONObject转换,而是返回一个实际类的Object,因为spring内部使用jackson,这是比org.json更强大的JSON框架

请在实体类中尝试此操作。它解决了这个问题。

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 

最新更新