`

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;

}

 

问题解决

分享到:
评论

相关推荐

    spring Mvc配置xml使ResponseBody返回Json的方法示例

    主要给大家介绍了关于spring Mvc配置xml使ResponseBody返回Json的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

    spring mvc 使用jquery 传json值给Controller时需要解决的问题

    NULL 博文链接:https://effort0829.iteye.com/blog/2111511

    Spring mvc 接收json对象

    本文通过代码实例介绍spring mvc 接收json数据的方法,具体详情如下所示: 接收JSON 使用 @RequestBody 注解前台只需要向 Controller 提交一段符合格式的 JSON,Spring 会自动将其拼装成 bean。 1)在上面的项目中...

    spring MVC学习笔记

    spring mvc的学习笔记,主要讲解了基于xml的配置,和如何增加spring mvc拦截器,如何返回json报文

    SpringMVC返回json数据的三种方式

    Spring MVC属于SpringFrameWork的后续产品,已经融合在...1、第一种方式是spring2时代的产物,也就是每个json视图controller配置一个Jsoniew。 如:&lt;bean id=”defaultJsonView” class=”org.springframework.w

    Spring MVC参数传递

    Spring MVC传参是Spring学习中的比较容易混淆的地方,因为传参方式种类多,接受参数种类多,初学者较容易因为不懂HTTP参数传递原理,而摸不着头脑,该篇文章介绍Spring MVC参数绑定的几种方式(包括json和普通文本)

    spring mvc 3.2 参考文档

    Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置...

    Spring MVC 关于controller的字符编码问题

    在使用springMVC框架构建web应用,客户端常会请求字符串、整型、json等格式的数据,通常使用@ResponseBody注解使 controller回应相应的数据而不是去渲染某个页面。

    spring_MVC源码

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...

    spring mvc 组合mybatis框架实例详解

    本项目采用 maven 结构,主要演示了 spring mvc + mybatis,controller 获取数据后以json 格式返回数据。对spring mvc 组合mybatis的方法感兴趣的朋友可以参考下本

    springMVC3使用@ResponseBody向浏览器返回 json,注意区分jar包前缀

    &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt; &lt;property name="messageConverters"&gt; &lt;list&gt; &lt;!-- Support JSON --&gt; &lt;bean class="org.spring...

    Spring MVC+Maven+Eclipse工程框架

    使用Eclipse配置一个SpringMVC + Maven的工程。具体的步骤可以参考博客:http://limingnihao.iteye.com/blog/830409。 附件对于一些注解、json的配置已经完成,并且有一个TestController可以做测试。

    Picker:Spring MVC +Hibernate

    #PickerPicker是一个基于实体书的知识分享平台。在Picker上,您可以自由地分享阅读过程中的心得... .controller.rest(RESTful): 提供了返回Json格式的url,安卓端和web端的ajax使用。Service层 .service: Service层对

    SpringMVC+ExtJS完美集成(数据传输方式为JSON和model)

    SpringMVC+ExtJS完美集成(数据传输方式为JSON和model),同时还支持controller页面配置、页面消息可配置(类似于国际化)、intercepter示例。

    spirngmvc js传递复杂json参数到controller的实例

    Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是application/json,因此发送POST请求时需要设置请求报文头信息,否则Spring ...

    spring-boot-mvc-jpa-data-mysql

    jpa-data-mysql这是一个用gradle构建的一个spring boot应用程序,该程序包含了spring mvc 4,spring data jpa 和 jsp,主要展示了Controller,RestController 的用法,以及数据绑定,分页,持久化,控制器返回json或...

    spring-boot-reference.pdf

    Auto-configured Spring REST Docs Tests with Mock MVC Auto-configured Spring REST Docs Tests with REST Assured 43.3.20. User Configuration and Slicing 43.3.21. Using Spock to Test Spring Boot ...

    smart-doc是一款同时支持JAVA REST API和Apache Dubbo RPC接口文档生成的工具,.rar

    支持Spring MVC、Spring Boot、Spring Boot Web Flux(controller书写方式)、Feign。 支持Callable、Future、CompletableFuture等异步接口返回的推导。 支持JavaBean上的JSR303参数校验规范,包括分组验证。 对JSON...

    Spring.3.x企业应用开发实战(完整版).part2

    9.2.5 Spring使用ThreadLocal解决线程安全问题 9.3 Spring对事务管理的支持 9.3.1 事务管理关键抽象 9.3.2 Spring的事务管理器实现类 9.3.3 事务同步管理器 9.3.4 事务传播行为 9.4 编程式的事务管理 9.5 使用XML...

    SpringMvcGuide:一个用来记录SpringMVC细节的项目

    SpringMVC的相关用法主要内容@RequestMapping注解中相关参数的意义参考RequestMappingController类.Controller中方法的参数可以定义的类型统计普通常用的基本参数没有写, 介绍了一些稍微冷门但是还有点用的, 参考...

Global site tag (gtag.js) - Google Analytics