I have set response as JSON but get this
Could not write JSON: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable 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;
}
This will throw an error, as JSONObject
does not expose default getter
.
Although a workaround can be done to avoid this thing.
You need to change ResponseGenerator
class to accept Map<String, Object>
instead of JSONObject
.
Now change this line:
responseGenerator.setJSONData(data);
to this:
responseGenerator.setJSONData(data.toMap());
I hope this should work.
P.S.: My recommendation would be to remove JSONObject
conversion and instead return an Object of actual class,as internally spring uses jackson
, which is more powerful JSON
framework then org.json
Try with this in the entity class. It solved that issue.
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With