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

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

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 注解是一个好的做法,它可以确保参数的正确绑定和映射。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>