fastjson 在web请求的时候报错,"java.lang.Integer cannot be cast to java.lang.Long

hiyou 发布于 2017/05/05 16:58
阅读 1K+
收藏 0

web请求,请求参数

{name: "zhansan", description: "飒飒大讲述了克里斯",…}
description:"飒飒大讲述了克里斯"
functions:[{id: 1}, {id: 41}, {id: 47}, {id: 49}, {id: 50}]
name:"zhansan"

后台接收参数方法:


    @RequestMapping("/saveOrUpdate")
    public @ResponseBody ResponseEntity<String> saveOrUpdate(@RequestBody Role role) {
        roleService.saveOrUpdate(role);
        return ResponseEntity.ok("success");
    }

返回结果:

{
	"timestamp":1493972641002,
	"status":500,
	"error":"Internal Server Error",
	"exception":"com.alibaba.fastjson.JSONException",
	"message":"java.lang.Integer cannot be cast to java.lang.Long",
	"path":"/role/saveOrUpdate.htm"
}

Role 对象中存在一个function对象的集合,function对象中id 的属性类型是Long类型,结果报错java.lang.Integer cannot be cast to java.lang.Long,这个问题是怎么导致的,而且这个问题是有时候会出现,有时候不会出现,怎么解决?
 

package com.control.back.halo.manage.entity;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import com.control.back.halo.basic.entity.BaseEntity;

@Entity
@Table(name = "sys_role")
public class Role extends BaseEntity {

    /**
     * 
     */
    private static final long       serialVersionUID = 820694530776191567L;

    // fields
    private java.lang.String        name;
    private java.lang.String        description;

    // collections
    @ManyToMany(mappedBy = "roles")
    private java.util.Set<Admin>    admins;
    
    @ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
    @JoinTable(name = "sys_role_function", joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = { @JoinColumn(name = "function_id") })
    private java.util.Set<Function> functions;

    /**
     * Return the value associated with the column: NAME
     */
    public java.lang.String getName() {
        return name;
    }

    /**
     * Set the value related to the column: NAME
     * 
     * @param name
     *            the NAME value
     */
    public void setName(java.lang.String name) {
        this.name = name;
    }

    /**
     * Return the value associated with the column: DESCRIPTION
     */
    public java.lang.String getDescription() {
        return description;
    }

    /**
     * Set the value related to the column: DESCRIPTION
     * 
     * @param description
     *            the DESCRIPTION value
     */
    public void setDescription(java.lang.String description) {
        this.description = description;
    }

    /**
     * Return the value associated with the column: admins
     */
    public java.util.Set<Admin> getAdmins() {
        return admins;
    }

    /**
     * Set the value related to the column: admins
     * 
     * @param admins
     *            the admins value
     */
    public void setAdmins(java.util.Set<Admin> admins) {
        this.admins = admins;
    }

    /**
     * Return the value associated with the column: functions
     */
    public java.util.Set<Function> getFunctions() {
        return functions;
    }

    /**
     * Set the value related to the column: functions
     * 
     * @param functions
     *            the functions value
     */
    public void setFunctions(java.util.Set<Function> functions) {
        this.functions = functions;
    }
}

 

加载中
0
yehun
yehun

Integer放不下时间戳, 改成Long即可

0
hiyou
hiyou

引用来自“夜丶魂”的评论

Integer放不下时间戳, 改成Long即可

没有时间戳

0
yehun
yehun

这个timestamp?

hiyou
hiyou
我说的不是时间戳类型转换
0
yehun
yehun

建议把实体类贴出来, 不然数据类型都看不到, 异常描述的很清楚, 类型转换错误

hiyou
hiyou
回复 @夜丶魂 : 你看吧
yehun
yehun
回复 @hiyou : Role实体类的代码贴出来
hiyou
hiyou
你能不能仔细看看
0
yehun
yehun

看不出什么异常Function和Admin都看不到, 把代码剥离出来, 直接用fastjson把你那段json转换成Role测试下

yehun
yehun
回复 @hiyou : 不用谢, 毕竟没有帮到你
hiyou
hiyou
恩,谢谢指点
返回顶部
顶部