admin 管理员组

文章数量: 1086019


2024年3月8日发(作者:press怎么记忆)

springmvccontroller返回json出错解决

1、出错的地方:

@RequestMapping(value = "/query")

@JsonSerialize(include=_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 = ogInfoCount(info);

if (totalCount == null) {

totalCount = 0L;

}

List list = ogInfo(info);

JSONObject retData = new JSONObject();

("success", true);

("data", list);

("error", "");

("totalRecords", totalCount);

return retData;

}

2、结果报错:

Apache Tomcat/7.0.42 - Error </p><p style="text-indent: 2em;";>report

HTTP Status 500 - Could not write

JSON: Object is null (through reference chain:

ject["data"]->ray[0]->ject["createTime"]->ll["empty"]); nested exception is

ppingException: Object is null (through

reference chain:

ject["data"]->ray[0]->ject["createTime"]->ll["empty"])


noshade="noshade">

type Exception report

message

Could not write JSON: Object is null (through reference chain:

ject["data"]->ray[0]->ject["createTime"]->ll["empty"]); nested exception is

ppingException: Object is null (through

reference chain:

ject["data"]->ray[0]->ject["createTime"]->ll["empty"])

description The server

encountered an internal error that prevented it from fulfilling this

request.

exception

ssageNotWritableException: Could not write JSON: Object is null (through reference chain: 

ject["data"]->ray[0]->ject["createTime"]->ll["empty"]); nested exception is

ppingException: Object is null (through

reference chain:

ject["data"]->ray[0]->ject["createTime"]->ll["empty"])

nternal(:203)

(:179)

ithMessageConverters(:148)

ithMessageConverters(:90)

ReturnValue(:189)

ReturnValue(:69)

AndHandle(:122)

HandleMethod(:745)

Internal(:686)

(:80)

atch(:925)

ice(:856)

sRequest(:936)

(:838)

e(:647)

e(:812)

e(:728)

er(:61)

eChain(:108)

erInternal(:137)

er(:125)

er(:66)

eChain(:449)

ctShiroFilter$(:365)

(:90)

(:83)

e(:383)

erInternal(:362)

er(:125)

Delegate(:343)

er(:260)

erInternal(:88)

er(:107)

root cause

ppingException: Object is null 

(through reference chain:

ject["data"]->ray[0]->ject["createTime"]->ll["empty"])

thPath(:218)

thPath(:183)

dThrow(:140)

3原因分析:

可能是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 = ogInfoCount(info);

if (totalCount == null) {

totalCount = 0L;

}

List list = ogInfo(info);

Map result = new HashMap();

("success", true);

("data", list);

("error", "");

("totalRecords", totalCount);

return result;

}

4、问题解决


本文标签: 出错 解决 序列化 问题