使用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<Type> 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);
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Zp_insist/article/details/122180465