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

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

原文地址:https://blog.csdn.net/weixin_44339107/article/details/132296446

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_42594.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注