春季HATEOAS和Java 8:我是否被迫抛出异常?

huangapple 未分类评论43阅读模式
英文:

Spring hateoas and java 8: am I forced to throw exception?

问题

以下是翻译好的部分:

我只想在我的回答中添加链接,所以以下的 Java 7 代码是完全正确的:

for (PetModel pet : pets) {
    pet.add(linkTo(methodOn(PetController.class).getPetById(pet.getId())).withSelfRel());
}

而这段代码则要求我处理 getPetById 可能抛出的异常,尽管在这个片段中它永远不会抛出异常:

pets.forEach((pet) -> pet.add(linkTo(methodOn(PetController.class).getPetById(pet.getId())).withSelfRel()));

为什么会这样呢?

英文:

I just want to add links to my response, so the following code in java 7 is perfectly fine:

for(PetModel pet : pets){
    pet.add(linkTo(methodOn(PetController.class).getPetById(pet.getId())).withSelfRel());
}

This one forces me to handle the exception that getPetById could throw, even though it will never do that in this snippet:

pets.forEach( (pet) -> pet.add(linkTo(methodOn(PetController.class).getPetById(pet.getId())).withSelfRel()));

why is that?

huangapple
  • 本文由 发表于 2020年4月10日 03:53:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/61129195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定