开发者社区> 问答> 正文

fastjson 在web请求的时候报错,"java.lang.Integer ?400报错

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

 

展开
收起
爱吃鱼的程序员 2020-06-08 14:58:50 802 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

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

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

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

    这个timestamp?

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

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

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

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

    回复 @hiyou:不用谢,毕竟没有帮到你恩,谢谢指点
    2020-06-08 14:59:01
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载