如何使用可选的orelsethrow

q3aa0525  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(117)

java 11规范(openjdk11)
Spring Boot2.3.6
智能
这是。。
显示图像

@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);
    }
}

还有阿尔塞
显示图像

@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);
    }
}

但它是有效的。。
为什么显示红色下划线?
显示消息1
显示消息2

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题