Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not write JSON: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer

Tags:

java

json

spring

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;

}
like image 398
Sandip Solanki Avatar asked Aug 28 '17 09:08

Sandip Solanki


2 Answers

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

like image 57
Sachin Gupta Avatar answered Nov 03 '22 00:11

Sachin Gupta


Try with this in the entity class. It solved that issue.

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 
like image 1
Pramuditha Avatar answered Nov 03 '22 00:11

Pramuditha