没有为类org.json.JSONArray找到序列化程序,也没有发现创建BeanSerializer的属性

时间:2017-06-27 01:45:25

标签: java json spring datatables

  

没有为类org.json.JSONObject找到序列化程序,也没有发现创建BeanSerializer的属性(为了避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)

当我试图获取要传递到我的数据表的JSON数组时,我收到上述错误消息。

我按照之前的StackOverflow问题(No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer)来查看它是否会有所帮助,但我仍遇到同样的问题。

@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;
}

这是JSONArray的输出(目前我只有一个JSON对象,但希望将来有多个JSON对象)

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

我修改了我的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" />

创建了一个名为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);
 }
}

0 个答案:

没有答案