『博客开发日记-后台』之编辑更新文章接口的实现

本文最后更新于 2026年4月17日 凌晨

编辑更新文章接口的实现


编辑更新文章接口的需求

根据文章id获取文章信息(必须要有id才能进行更新操作)

要可以更新缩略图或文章内容里的图片或者文件,即正确上传oss中的更新操作

支持更新文章基本信息

更新标签关联关系

代码实现

下面只展示服务类里的实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
///编辑/更新文章
@Override
@Transactional
public ResponseResult updatePosts(Long id, UpdatePostsDto updatePostsDto)
{
if (id == null) {
throw new RuntimeException("更新文章时ID不能为空");
}

// 确保使用路径参数的ID
updatePostsDto.setId(id);

// 获取旧文章信息
Article oldArticle = articleService.getById(id);
if (oldArticle == null) {
throw new RuntimeException("文章不存在");
}

// 处理文章内容中的图片
String content = updatePostsDto.getContent();
List<String> oldContentImages = new ArrayList<>();
List<String> newContentImages = new ArrayList<>();

// 提取旧内容中的图片URL
if (StringUtils.hasText(oldArticle.getContent())) {
oldContentImages = ImageUrlUtils.extractImageUrls(oldArticle.getContent());
}

// 处理新内容中的临时图片
if (ImageUrlUtils.containsTempImages(content)) {
content = ossFileService.processContentImages(content);
updatePostsDto.setContent(content);
}

// 提取新内容中的图片URL
if (StringUtils.hasText(content)) {
newContentImages = ImageUrlUtils.extractImageUrls(content);
}

// 删除不再使用的内容图片
for (String oldImageUrl : oldContentImages) {
if (!newContentImages.contains(oldImageUrl)) {
ossFileService.deleteFileByUrl(oldImageUrl);
log.info("删除旧的内容图片: {}", oldImageUrl);
}
}

// 处理缩略图
String newThumbnail = updatePostsDto.getThumbnail();
String oldThumbnail = oldArticle.getThumbnail();

if (StringUtils.hasText(newThumbnail) && newThumbnail.contains("temp/")) {
newThumbnail = ossFileService.moveTempToFormal(newThumbnail);
updatePostsDto.setThumbnail(newThumbnail);
}

// 如果缩略图发生变化,删除旧缩略图
if (StringUtils.hasText(oldThumbnail) &&
StringUtils.hasText(newThumbnail) &&
!oldThumbnail.equals(newThumbnail)) {
ossFileService.deleteFileByUrl(oldThumbnail);
log.info("删除旧的缩略图: {}", oldThumbnail);
}

// 更新文章基本信息
Article article = BeanCopyUtils.copyBean(updatePostsDto, Article.class);
articleService.updateById(article);

// 更新标签关联关系
// 先删除旧的关联关系
LambdaQueryWrapper<ArticleTag> deleteWrapper = new LambdaQueryWrapper<>();
deleteWrapper.eq(ArticleTag::getArticleId, updatePostsDto.getId());
articleTagMapper.delete(deleteWrapper);

// 添加新的关联关系
if (updatePostsDto.getTags() != null && !updatePostsDto.getTags().isEmpty()) {
List<ArticleTag> articleTags = updatePostsDto.getTags().stream()
.map(tagId -> new ArticleTag(article.getId(), tagId))
.collect(Collectors.toList());
articleTagService.saveBatch(articleTags);
}

return ResponseResult.okResult();
}

新建 UpdatePostsDto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 更新文章请求DTO
*/
@Data
@ApiModel(description = "更新文章请求对象")
public class UpdatePostsDto {

@ApiModelProperty(value = "文章ID", required = true, example = "1")
private Long id;

@ApiModelProperty(value = "文章标题", required = true, example = "文章标题")
private String title;

@ApiModelProperty(value = "文章内容(HTML格式)", required = true)
private String content;

@ApiModelProperty(value = "文章摘要", example = "文章摘要")
private String summary;

@ApiModelProperty(value = "缩略图URL", example = "https://example.com/image.jpg")
private String thumbnail;

@ApiModelProperty(value = "分类ID", required = true, example = "1")
private Long categoryId;

@ApiModelProperty(value = "是否置顶(0-否,1-是)", example = "0")
private String isTop;

@ApiModelProperty(value = "状态(0-已发布,1-草稿)", example = "0")
private String status;

@ApiModelProperty(value = "是否允许评论(0-否,1-是)", example = "1")
private String isComment;

@ApiModelProperty(value = "标签ID列表")
private List<Long> tags;
}



PS:该系列只做为作者学习开发项目做的笔记用

不一定符合读者来学习,仅供参考


预告

后续会记录博客的开发过程

每次学习会做一份笔记来进行发表

“一花一世界,一叶一菩提”


版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/


『博客开发日记-后台』之编辑更新文章接口的实现
http://example.com/2026/04/13/『博客开发日记-后台』之编辑更新文章接口的实现/
作者
云梦泽
发布于
2026年4月13日
更新于
2026年4月17日
许可协议