spring mvc controller返回json出错解决

spring mvc 中写法

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

}

 

结果报错:

Apache Tomcat/7.0.42 - Error report

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"])


type Exception report

message 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"])

description The server encountered an internal error that prevented it from fulfilling this request.

exception

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)

org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)

org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)

org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)

org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)

org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)

org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)

org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)

org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)

org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)

org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)

org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)

org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)

org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

root cause

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.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218)

org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183)

org.codehaus.jackson.map.ser.std.SerializerBase.wrapAndThrow(SerializerBase.java:140)

 

 

可能是json序列化的问题

代码修改为

@RequestMapping(value = "/query")

public @ResponseBody

Map 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 list = logMoniterService.queryLogInfo(info);

 

Map result = new HashMap();

 

result.put("success", true);

result.put("data", list);

result.put("error", "");

result.put("totalRecords", totalCount);

return result;

}

 

问题解决

你可能感兴趣的:(编程问题解决)