springmvccontroller返回json出错解决
1、出错的地方:
@RequestMapping(value = "/query")
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) public @ResponseBody
JSONObject query(@RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime,
@RequestParam("oid") String oid, @RequestParam("cid") Integer cid, @RequestParam("pay_channel") String payChannel,
@RequestParam("device_type") String deviceType,
@RequestParam("phone") Integer phone,
@RequestParam("perPage") Integer pageSize,
@RequestParam("skipIndex") Integer startIndex) throws Exception {
LogInfo info = populateQueryParam(startTime, endTime, oid, cid, payChannel, deviceType, phone, pageSize, startIndex);
// 总记录数
Long totalCount = logMoniterService.queryLogInfoCount(info);
if (totalCount == null) {
totalCount = 0L;
}
List<LogInfo> list = logMoniterService.queryLogInfo(info);
JSONObject retData = new JSONObject();
retData.put("success", true);
retData.put("data", list);
retData.put("error", "");
retData.put("totalRecords", totalCount);
return retData;
}
2、结果报错:
<html><head><title>Apache Tomcat/7.0.42 - Error
report</title><style><!--H1
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY
{font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B
{font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P
{font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color :
#525D76;}--></style> </head><body><h1>HTTP Status 500 - Could not write JSON: Object is null (through reference chain:
net.sf.json.JSONObject["data"]->net.sf.json.JSONArray[0]->net.sf.json.JSONObject["createTime"]->net.sf.json.JSONNull["empty"]); nested exception is
org.codehaus.jackson.map.JsonMappingException: Object is null (through reference chain:
net.sf.json.JSONObject["data"]->net.sf.json.JSONArray[0]->net.sf.json.JSONObject["createTime"]->net.sf.json.JSONNull["empty"])</h1><HR size="1"
noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u>Could not write JSON: Object is null (through reference chain: net.sf.json.JSONObject["data"]->net.sf.json.JSONArray[0]->net.sf.json.JSONObject["createTime"]->net.sf.json.JSONNull["empty"]); nested exception is
org.codehaus.jackson.map.JsonMappingException: Object is null (through reference chain:
net.sf.json.JSONObject["data"]->net.sf.json.JSONArray[0]->net.sf.json.JSONObject["createTime"]->net.sf.json.JSONNull["empty"])</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b>
<pre>org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Object is null (through reference chain: net.sf.json.JSONObject["data"]->net.sf.json.JSONArray[0]->net.sf.json.JSONObject["createTime"]->net.sf.json.JSONNull["empty"]); nested exception is
org.codehaus.jackson.map.JsonMappingException: Object is null (through reference chain:
net.sf.json.JSONObject["data"]->net.sf.json.JSONArray[0]->net.sf.json.JSONObject["createTime"]->net.sf.json.JSONNull["empty"])
org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.writeInternal(MappingJacksonHttpMessageConverter.java:203) org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:90)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:189)
org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)