使用SpringBoot报错:Inferred type ‘S‘ for type parameter ‘S‘ is not within its bound。【解决办法】

使用SpringBoot报错:Inferred type ‘S‘ for type parameter ‘S‘ is not within its bound。【解决办法】,第1张

使用SpringBoot报错:Inferred type ‘S‘ for type parameter ‘S‘ is not within its bound。【解决办法】 ❌一、错误展示

使用SpringBoot时出现如下错误:

Inferred type ‘S’ for type parameter ‘S’ is not within its bound


错误代码:

    public Type updateType(Long id, Type type) {
         Optional t = typeDao.findById(id);
        if (t == null){
            throw new NotFoundException("不存在该类型");
        }
        BeanUtils.copyProperties(type,t);
        return typeDao.save(t);
    }
✅二、解决办法

第一种:

将typeDao.findById(id);改为typeDao.findById(id) .orElse(null);

    public Type updateType(Long id, Type type) {
        Type t = typeDao.findById(id).orElse(null);
        if (t == null){
            throw new NotFoundException("不存在该类型");
        }
        BeanUtils.copyProperties(type,t);
        return typeDao.save(t);
    }

第二种:

将typeDao.findById(id);改为typeDao.findById(id) .get();

    public Type updateType(Long id, Type type) {
        Type t = typeDao.findById(id).get();
        if (t == null){
            throw new NotFoundException("不存在该类型");
        }
        BeanUtils.copyProperties(type,t);
        return typeDao.save(t);
    }

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5681632.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存