【问题标题】:Could not write JSON: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer无法写入 JSON:没有为类 org.json.JSONObject 找到序列化程序,也没有发现用于创建 BeanSerializer 的属性
【发布时间】: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。

标签: java json spring


【解决方案1】:

这将引发错误,因为JSONObject 不会公开默认的getter。 虽然可以采取一种解决方法来避免这种情况。

您需要将ResponseGenerator 类更改为接受Map&lt;String, Object&gt; 而不是JSONObject。 现在改变这一行:

responseGenerator.setJSONData(data);

到这里:

 responseGenerator.setJSONData(data.toMap());

我希望这应该可行。

P.S.:我的建议是删除 JSONObject 转换并返回一个实际类的对象,因为内部 spring 使用 jackson,它比 JSON 框架更强大 org.json

【讨论】:

    【解决方案2】:

    在实体类中试试这个。它解决了这个问题。

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

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 2014-11-21
      • 2012-10-25
      • 2021-06-26
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 2020-04-22
      • 2014-09-19
      相关资源
      最近更新 更多