英文:
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?
专注分享java语言的经验与见解,让所有开发者获益!
评论