服务器报500错误 No primary or single unique constructor found for interface java.util.List

文章讲述了前端如何通过API批量删除日志记录,遇到`IllegalStateException`的问题,原因在于SpringBoot尝试为接口类型创建实例。解决方案是在`@DeleteMapping`方法中使用`@RequestParam`注解明确指定请求参数名和类型,确保正确绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 批量删除日志记录 前端请求

URL:http://localhost:8080/system/log?ids=3,4,5

Method:DELETE

//批量删除日志记录
    deleteLogs() {
      let url = '/system/log'
      if (this.currentRow == null && this.multipleSelection.length == 0) {
        this.$message.warning("请先选择记录")
        return;
      } else if (this.multipleSelection.length > 0) {
        url += '?ids=' + this.multipleSelection;
      } else if (this.currentRow && this.multipleSelection.length == 0) {
        url += '?ids=' + this.currentRow.id;
      }
      this.$confirm('此操作将永久删除该记录,是否继续?', '提示', {type: "warning"}).then(() => {
        this.deleteRequest(url).then(resp => {
          if (resp) {
            this.getLogs();
          }
        });
      }).catch(() => {
        this.$message.info('已取消当前操作~')
      });
    }

后端接口:

 /**
     * 根据id删除日志记录
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deleteLogsByIds(List<Long> ids){
        boolean flag = operatorLogService.removeByIds(ids);
        if (flag){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

报错:java.lang.IllegalStateException: No primary or single unique constructor found for interface java.util.List

解决方法:添加@RequestParam注解

 @DeleteMapping
    public Result deleteLogsByIds(@RequestParam List<Integer> ids){
        boolean flag = operatorLogService.removeByIds(ids);
        if (flag){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

原因分析: 由于 Spring Boot 默认情况下会尝试使用请求参数的值来创建方法参数,但对于接口类型(如 java.util.List)而言,它无法直接创建一个接口的实例。

通过使用 @RequestParam 注解,明确指定了请求参数的名称,并将请求参数的值绑定到方法参数上,从而避免了这个错误。@RequestParam 注解告诉 Spring Boot 去查找请求参数的值,并将值映射到方法参数上,而不会尝试创建一个接口的实例。

所以,如果你的方法参数是一个具体的类类型(如 List<Long>),那么 Spring Boot 可以通过请求参数的值来创建一个具体的类的实例。但是对于接口类型,它无法这样做,因此需要使用 @RequestParam 注解来明确指定参数的名称和类型,以避免错误。总之,为了避免这个错误,当你的方法参数类型是接口类型时,使用 @RequestParam 注解是一个好的做法,它可以确保参数的正确绑定和映射。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值