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

}

结果报错:

<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[&quot;data&quot;]-&gt;net.sf.json.JSONArray[0]-&gt;net.sf.json.JSONObject[&quot;createTime&quot;]-&gt;net.sf.json.JSONNull[&quot;empty&quot;]); nested exception is org.codehaus.jackson.map.JsonMappingException: Object is null (through reference chain: net.sf.json.JSONObject[&quot;data&quot;]-&gt;net.sf.json.JSONArray[0]-&gt;net.sf.json.JSONObject[&quot;createTime&quot;]-&gt;net.sf.json.JSONNull[&quot;empty&quot;])</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[&quot;data&quot;]-&gt;net.sf.json.JSONArray[0]-&gt;net.sf.json.JSONObject[&quot;createTime&quot;]-&gt;net.sf.json.JSONNull[&quot;empty&quot;]); nested exception is org.codehaus.jackson.map.JsonMappingException: Object is null (through reference chain: net.sf.json.JSONObject[&quot;data&quot;]-&gt;net.sf.json.JSONArray[0]-&gt;net.sf.json.JSONObject[&quot;createTime&quot;]-&gt;net.sf.json.JSONNull[&quot;empty&quot;])</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[&quot;data&quot;]-&gt;net.sf.json.JSONArray[0]-&gt;net.sf.json.JSONObject[&quot;createTime&quot;]-&gt;net.sf.json.JSONNull[&quot;empty&quot;]); nested exception is org.codehaus.jackson.map.JsonMappingException: Object is null (through reference chain: net.sf.json.JSONObject[&quot;data&quot;]-&gt;net.sf.json.JSONArray[0]-&gt;net.sf.json.JSONObject[&quot;createTime&quot;]-&gt;net.sf.json.JSONNull[&quot;empty&quot;])

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)

</pre></p><p><b>root cause</b> <pre>org.codehaus.jackson.map.JsonMappingException: Object is null (through reference chain: net.sf.json.JSONObject[&quot;data&quot;]-&gt;net.sf.json.JSONArray[0]-&gt;net.sf.json.JSONObject[&quot;createTime&quot;]-&gt;net.sf.json.JSONNull[&quot;empty&quot;])

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<String, Object> 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);

Map<String, Object> result = new HashMap<String, Object>();

 

result.put("success", true);

result.put("data", list);

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

result.put("totalRecords", totalCount);

return result;

}

问题解决

猜你喜欢

转载自magicyang919.iteye.com/blog/2027000