Intellij idea 推断类型';S';对于类型参数';S';不在其范围之内为什么? @PutMapping(value=“/{id}”,consumes=“application/json”,products=“application/json”) @ResponseStatus(HttpStatus.无内容) public void updateHero(@PathVariable Long id,@RequestBody-Hero){ //首先检索英雄。这是确保英雄已经存在的唯一方法 //为了节约。 可选currentHero=heroRepository.findById(id); 如果(currentHero==null){ 抛出新的ResourceNotFoundException(“id=“+id”未找到英雄); } debug(“updateHero:将名称从{}修改为{}”,currentHero.getName(),hero.getName()); currentHero.setName(hero.getName()); this.heroRepository.saveAll(currentHero); }

Intellij idea 推断类型';S';对于类型参数';S';不在其范围之内为什么? @PutMapping(value=“/{id}”,consumes=“application/json”,products=“application/json”) @ResponseStatus(HttpStatus.无内容) public void updateHero(@PathVariable Long id,@RequestBody-Hero){ //首先检索英雄。这是确保英雄已经存在的唯一方法 //为了节约。 可选currentHero=heroRepository.findById(id); 如果(currentHero==null){ 抛出新的ResourceNotFoundException(“id=“+id”未找到英雄); } debug(“updateHero:将名称从{}修改为{}”,currentHero.getName(),hero.getName()); currentHero.setName(hero.getName()); this.heroRepository.saveAll(currentHero); },intellij-idea,Intellij Idea,为什么它在this.heroRepository.saveAll(currentHero)上显示类型参数“S”的推断类型“S”不在其范围内;应该扩展'com.example.Schnittstelle.Hero' 我在google上找不到任何解决方案…saveAll函数需要一个Iterable。我们迭代的值的类型应该是一个Hero,但在本例中,您发送的是一个可选值,这就是为什么会出现错误 @PutMapping(value = "/{id}", consumes = &quo

为什么它在
this.heroRepository.saveAll(currentHero)上显示类型参数“S”的推断类型“S”不在其范围内;应该扩展'com.example.Schnittstelle.Hero'

我在google上找不到任何解决方案…

saveAll
函数需要一个Iterable。我们迭代的值的类型应该是一个
Hero
,但在本例中,您发送的是一个可选值,这就是为什么会出现错误

@PutMapping(value = "/{id}", consumes = "application/json", produces = "application/json")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void updateHero(@PathVariable Long id, @RequestBody Hero hero) {
        // Retrieve hero first. This is the only way to ensure hero already exists prior
        // to saving.
        Optional<Hero> currentHero = heroRepository.findById(id);
        if (currentHero == null) {
            throw new ResourceNotFoundException("Hero is not found for id=" + id);
        }
        LOG.debug("updateHero: modified name from {} to {}", currentHero.getName(), hero.getName());
        currentHero.setName(hero.getName());
        this.heroRepository.saveAll(currentHero);
    }

@PutMapping(value=“/{id}”,consumes=“application/json”,products=“application/json”)
@ResponseStatus(HttpStatus.无内容)
public void updateHero(@PathVariable Long id,@RequestBody-Hero){
//首先检索英雄。这是确保英雄已经存在的唯一方法
//为了节约。
可选currentHeroOptional=heroRepository.findById(id);
如果(!currentHeroOptional.isPresent()){
抛出新的ResourceNotFoundException(“id=“+id”未找到英雄);
}
Hero currentHero=currentHeroOptional.get();
debug(“updateHero:将名称从{}修改为{}”,currentHero.getName(),hero.getName());
currentHero.setName(hero.getName());
this.heroRepository.saveAll(Collections.singletonList(currentHero));
}

函数的
saveAll
需要一个Iterable。我们迭代的值的类型应该是一个
Hero
,但在本例中,您发送的是一个可选值,这就是为什么会出现错误

@PutMapping(value = "/{id}", consumes = "application/json", produces = "application/json")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void updateHero(@PathVariable Long id, @RequestBody Hero hero) {
        // Retrieve hero first. This is the only way to ensure hero already exists prior
        // to saving.
        Optional<Hero> currentHero = heroRepository.findById(id);
        if (currentHero == null) {
            throw new ResourceNotFoundException("Hero is not found for id=" + id);
        }
        LOG.debug("updateHero: modified name from {} to {}", currentHero.getName(), hero.getName());
        currentHero.setName(hero.getName());
        this.heroRepository.saveAll(currentHero);
    }

@PutMapping(value=“/{id}”,consumes=“application/json”,products=“application/json”)
@ResponseStatus(HttpStatus.无内容)
public void updateHero(@PathVariable Long id,@RequestBody-Hero){
//首先检索英雄。这是确保英雄已经存在的唯一方法
//为了节约。
可选currentHeroOptional=heroRepository.findById(id);
如果(!currentHeroOptional.isPresent()){
抛出新的ResourceNotFoundException(“id=“+id”未找到英雄);
}
Hero currentHero=currentHeroOptional.get();
debug(“updateHero:将名称从{}修改为{}”,currentHero.getName(),hero.getName());
currentHero.setName(hero.getName());
this.heroRepository.saveAll(Collections.singletonList(currentHero));
}

currentHero是可选类型,如果要保存hero类的对象,可以使用
currentHero.get()获取hero的值。
此外,saveAll需要一个interable,并且只发送一个元素。尝试发送
Collections.singletonList(currentHero.get())
我之前尝试过类Hero,但后来我收到一个错误,告诉我必须将currentHero的类型更改为可选的…另外,如果我使用可选的,就没有getname和setName()方法了。如果currentHero的类型是可选的,您想保存类Hero的对象,您可以使用
currentHero.get()
获取hero的值。saveAll需要一个interable,您只发送一个元素。尝试发送
Collections.singletonList(currentHero.get())
我以前尝试过类Hero,但后来我收到一个错误,告诉我必须将currentHero的类型更改为可选…另外,如果我使用可选,则不再有getname和setName()方法


@PutMapping(value = "/{id}", consumes = "application/json", produces = "application/json")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void updateHero(@PathVariable Long id, @RequestBody Hero hero) {
        // Retrieve hero first. This is the only way to ensure hero already exists prior
        // to saving.
        Optional<Hero> currentHeroOptional = heroRepository.findById(id);
        if (!currentHeroOptional.isPresent()) {
            throw new ResourceNotFoundException("Hero is not found for id=" + id);
        }
        Hero currentHero = currentHeroOptional.get();
        LOG.debug("updateHero: modified name from {} to {}", currentHero.getName(), hero.getName());
        currentHero.setName(hero.getName());
        this.heroRepository.saveAll(Collections.singletonList(currentHero));
    }