1

No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

I am receiving the above error message when I'm trying to obtain a JSON array to pass into my datatable.

I followed along with this previous StackOverflow question (No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer) to see if it would help, but I am still encountering the same issue.

@RequestMapping(value= "/loc_list", method = RequestMethod.GET)
@ResponseBody
public JSONArray getLocList(){
    List<Locations> locList = locations.findAll();
    JSONArray locArray = new JSONArray();
    for(int i = 0; i < locList.size(); i++) {
        JSONObject locObj = new JSONObject();
        locObjObj.put("id", locObjList.get(i).getId());
        locObjObj.put("name", locObjList.get(i).getName());
        locObjObj.put("coordinates", locObjList.get(i).getCoordinates());
        locObjObj.put("description", locObjList.get(i).getDescription());
        System.out.println("Target # " + i + ": " + locObjObj.toString());
        locArray.put(locObjObj);
    }
    System.out.println("JSON Array " + locArray);
    return locArray;
}

This is the output of the JSONArray (at the moment I only have one JSON Object, but would like to have multiple JSON objects in the future)

[{"name":"xxxx","coordinates":"42.3601° N, 71.0589° W","description":"city","id":3}]

I've modified my applicationContext.xml

<context:component-scan base-package="com.A21.exceptions"/>

<mvc:annotation-driven>
 <mvc:message-converters>
  <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
          <property name="objectMapper" ref="jacksonObjectMapper" />
  </bean>
 </mvc:message-converters>
</mvc:annotation-driven>
<bean id="jacksonObjectMapper" class="com.A21.exceptions.MyJSONMapper" />

Created a new class also called MyJSONMapper.java:

package com.A21.exceptions;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class MyJSONMapper extends ObjectMapper {
 public MyJSONMapper() {
    this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
 }
}
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
A21
  • 181
  • 3
  • 18

0 Answers0