简体   繁体   English

如何使用 Optional orElseThrow

[英]How can I use Optional orElseThrow

spec java 11 (openjdk11)规格 java 11 (openjdk11)
spring boot 2.3.6 spring 开机 2.3.6
intellij智能

Here is..这是..
Show image显示图像

@Service
@RequiredArgsConstructor
public class PostService {

    private final PostRepository postRepository;
    public PostDto.Response getPostById(long postId) {
        Post post = postRepository.findById(postId)
                .orElseThrow(RuntimeException::new);    // <-- this line
        return new PostDto.Response(post);
    }
}

and alse还有
Show image显示图像

@Service
@RequiredArgsConstructor
public class PostService {

    private final PostRepository postRepository;
    public PostDto.Response getPostById(long postId) {
        Post post = postRepository.findById(postId)
                .orElseThrow(() -> new RuntimeException());  // <-- this line
        return new PostDto.Response(post);
    }
}

But It works..但它有效..
Why is that red underline displayed?为什么显示红色下划线?
Show Message1显示消息1
Show Message2显示消息2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM