feat: 实现API文档支持与系统优化
refactor(ArticleRepository): 修正@Param注解导入错误并优化查询方法 fix(ArticleService): 解决事务回滚问题并优化日志配置 feat(SecurityConfig): 添加Spring Security配置禁用默认认证 docs: 添加详细API文档README_API.md feat(HelpController): 实现Markdown文档渲染API style: 清理无用注释和导入 build: 更新pom.xml依赖和插件配置 chore: 优化application.properties配置
This commit is contained in:
@@ -12,30 +12,56 @@ import org.springframework.web.bind.annotation.*;
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文章控制器类,处理文章相关的HTTP请求
|
||||
* 提供文章的增删改查功能,以及按作者、分类和浏览量获取文章的接口
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/articles")
|
||||
@Validated
|
||||
public class ArticleController {
|
||||
|
||||
@Autowired
|
||||
private IArticleService articleService;
|
||||
private IArticleService articleService; // 注入文章服务接口
|
||||
|
||||
/**
|
||||
* 根据ID获取单个文章
|
||||
* @param id 文章ID
|
||||
* @return 返回包含文章信息的ResponseMessage对象
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public ResponseMessage<Article> getArticle(@PathVariable Integer id) {
|
||||
return articleService.getArticleById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有文章列表
|
||||
* @return 返回包含文章列表的ResponseMessage对象
|
||||
*/
|
||||
@GetMapping
|
||||
public ResponseMessage<List<Article>> getAllArticles() {
|
||||
return articleService.getAllArticles();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新文章
|
||||
* 仅限AUTHOR角色用户访问
|
||||
* @param articleDto 包含文章数据的DTO对象
|
||||
* @return 返回包含新创建文章信息的ResponseMessage对象
|
||||
*/
|
||||
@PostMapping
|
||||
@PreAuthorize("hasRole('AUTHOR')")
|
||||
public ResponseMessage<Article> createArticle(@Valid @RequestBody ArticleDto articleDto) {
|
||||
return articleService.saveArticle(articleDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新现有文章
|
||||
* 仅限AUTHOR或ADMIN角色用户访问
|
||||
* @param id 要更新的文章ID
|
||||
* @param articleDto 包含更新后文章数据的DTO对象
|
||||
* @return 返回包含更新后文章信息的ResponseMessage对象
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
@PreAuthorize("hasRole('AUTHOR') or hasRole('ADMIN')")
|
||||
public ResponseMessage<Article> updateArticle(
|
||||
@@ -44,22 +70,42 @@ public class ArticleController {
|
||||
return articleService.updateArticle(id, articleDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文章
|
||||
* 仅限AUTHOR或ADMIN角色用户访问
|
||||
* @param id 要删除的文章ID
|
||||
* @return 返回包含被删除文章信息的ResponseMessage对象
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
@PreAuthorize("hasRole('AUTHOR') or hasRole('ADMIN')")
|
||||
public ResponseMessage<Article> deleteArticle(@PathVariable Integer id) {
|
||||
return articleService.deleteArticle(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据作者ID获取其所有文章
|
||||
* @param authorId 作者ID
|
||||
* @return 返回包含文章列表的ResponseMessage对象
|
||||
*/
|
||||
@GetMapping("/author/{authorId}")
|
||||
public ResponseMessage<List<Article>> getArticlesByAuthor(@PathVariable Integer authorId) {
|
||||
return articleService.getArticlesByAuthor(authorId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据分类ID获取该分类下的所有文章
|
||||
* @param categoryId 分类ID
|
||||
* @return 返回包含文章列表的ResponseMessage对象
|
||||
*/
|
||||
@GetMapping("/category/{categoryId}")
|
||||
public ResponseMessage<List<Article>> getArticlesByCategory(@PathVariable Integer categoryId) {
|
||||
return articleService.getArticlesByCategory(categoryId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取浏览量最高的文章列表
|
||||
* @return 返回包含热门文章列表的ResponseMessage对象
|
||||
*/
|
||||
@GetMapping("/popular")
|
||||
public ResponseMessage<List<Article>> getMostViewedArticles() {
|
||||
return articleService.getMostViewedArticles();
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.qf.myafterprojecy.controller;
|
||||
|
||||
import com.qf.myafterprojecy.pojo.ResponseMessage;
|
||||
import org.commonmark.node.Node;
|
||||
import org.commonmark.parser.Parser;
|
||||
import org.commonmark.renderer.html.HtmlRenderer;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 帮助控制器类,处理前端调用api/help请求
|
||||
* 提供README_API.md文件的读取和返回功能
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/help")
|
||||
public class HelpController {
|
||||
|
||||
/**
|
||||
* 获取README_API.md文件内容
|
||||
* @return 返回包含README_API.md文件内容的ResponseMessage对象
|
||||
*/
|
||||
@GetMapping
|
||||
public ResponseMessage<String> getReadmeApi() {
|
||||
try {
|
||||
// 获取项目根目录
|
||||
String rootPath = System.getProperty("user.dir");
|
||||
// 构建README_API.md文件路径
|
||||
File readmeFile = new File(rootPath, "README_API.md");
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!readmeFile.exists() || !readmeFile.isFile()) {
|
||||
// 如果不存在,尝试使用类路径资源加载
|
||||
try {
|
||||
ClassPathResource resource = new ClassPathResource("README_API.md");
|
||||
String markdownContent = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()), StandardCharsets.UTF_8);
|
||||
// 将Markdown转换为HTML
|
||||
String htmlContent = convertMarkdownToHtml(markdownContent);
|
||||
return ResponseMessage.success(htmlContent, "获取API文档成功");
|
||||
} catch (IOException e) {
|
||||
return ResponseMessage.error("未找到README_API.md文件");
|
||||
}
|
||||
}
|
||||
|
||||
// 读取文件内容
|
||||
String markdownContent = new String(FileCopyUtils.copyToByteArray(new FileInputStream(readmeFile)), StandardCharsets.UTF_8);
|
||||
// 将Markdown转换为HTML
|
||||
String htmlContent = convertMarkdownToHtml(markdownContent);
|
||||
return ResponseMessage.success(htmlContent, "获取API文档成功");
|
||||
} catch (IOException e) {
|
||||
return ResponseMessage.error("读取README_API.md文件失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Markdown文本转换为HTML
|
||||
* @param markdown 原始Markdown文本
|
||||
* @return 转换后的HTML字符串
|
||||
*/
|
||||
private String convertMarkdownToHtml(String markdown) {
|
||||
Parser parser = Parser.builder().build();
|
||||
Node document = parser.parse(markdown);
|
||||
HtmlRenderer renderer = HtmlRenderer.builder().build();
|
||||
return renderer.render(document);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user