Spring Boot 构建 REST API 与响应式编程指南
1. 继续构建 REST API
在之前的基础上,我们继续完善 REST API,添加处理更新和删除操作的方法,同时创建根据 slug 属性读取文章的端点。
1.1 更新操作
在 PostController.java 类中创建更新方法:
@PutMapping
public PostDto updatePost(@RequestBody PostDto postDto) {
return postService.update(postDto);
}
使用 @PutMapping 注解表示这是一个更新操作,在方法内部调用 PostService.java 类中的 update 方法:
public PostDto update(PostDto postDto) {
Post post = postRepository.findById(postDto.getId())
.orElseThrow(() -> new SpringBlogException("Cannot find Post with Id " + postDto.getId()));
Post savedPost = postMapper.mapToPost(postDto);
saved
超级会员免费看
订阅专栏 解锁全文

49

被折叠的 条评论
为什么被折叠?



