后端接口使用实体类作为参数,实际传参只用到实体类的某个属性,前端调用后端接口报错处理。

报错

JSON parse error: Cannot construct instance of com.example.entity.Business (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (22)

这个错误是由于在将JSON数据解析为Java对象时,无法找到一个合适的构造函数或工厂方法来处理从Number值(22)开始的解析。要解决这个问题,您需要检查您的Business实体类,确保它有一个合适的构造函数或工厂方法来处理从Number值开始的解析。

后端接口

@DeleteMapping("/deleteBatch")
public Result deleteBatch(@RequestBody List<Business> ids) {
    businessService.deleteBatch(ids);
    return Result.success();
}

前端调用

this.$request.delete('/business/deleteBatch', {data: this.ids})...

实体类

public class Business {


    /**
     * 主键
     */
    private Integer id;

    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 名称
     */
    private String name;
    /**
     * logo
     */
    private String avatar;
    /**
     * 角色
     */
    private String role;
    /**
     * 电话
     */
    private String phone;
    /**
     * 简介
     */
    private String info;
    /**
     * 地址
     */
    private String address;
    /**
     * 营业证书
     */
    private String license;
    /**
     * 审核状态
     */
    private String status;
    /**
     * 营业时间
     */
    private String timeRange;
    /**
     * 类型
     */
    private String type;

    public Business(Integer id, String username, String password, String name, String avatar, String role, String phone, String info, String address, String license, String status, String timeRange, String type) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.name = name;
        this.avatar = avatar;
        this.role = role;
        this.phone = phone;
        this.info = info;
        this.address = address;
        this.license = license;
        this.status = status;
        this.timeRange = timeRange;
        this.type = type;
    }

    public Business(Integer id) {
        this.id = id;
    }

    public Business() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getLicense() {
        return license;
    }

    public void setLicense(String license) {
        this.license = license;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getTimeRange() {
        return timeRange;
    }

    public void setTimeRange(String timeRange) {
        this.timeRange = timeRange;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

解决方法

在对应的实体类中单独创建一个只有id的构造器

public Business(Integer id) {
        this.id = id;
    }
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值