基于javaweb+mysql的springboot在线小说阅读系统(前后端分离+java+vue+springboot+ssm+mysql+maven)
运行环境
Java≥8、MySQL≥5.7、Node.js≥10
开发工具
后端:eclipse/idea/myeclipse/sts等均可配置运行
前端:WebStorm/VSCode/HBuilderX等均可
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SpringBoot在线小说阅读系统(前后端分离+java+vue+springboot+ssm+mysql+maven)
一、项目简述
本系统功能包括: 普通用户端登录注册,小说的分类,日榜,月榜,年榜, 小说的阅读,分章节,小说的评论,收藏,推荐等等,以 及后台小说的维护,上架,编辑等等。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX (Webstorm也 行)+ Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts都支 持)。
项目技术: Springboot + Maven + Mybatis + Vue , B/S 模式+ Maven等等
@RestController
@RequestMapping("/api/follow")
@Api(tags = "共同前缀:/api/follow", description = "FollowController")
@Slf4j
public class FollowController {
@Autowired
UserService userService;
@Autowired
FollowService followService;
@PostMapping
@ApiOperation("新增Follow")
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody Follow follow) {
log.info("新增Follow");
System.out.println(follow);
if (follow.getId() != null) {
throw new ControllerException("id必须为null");
} else if (follow.getFollower_id() != null) {
throw new ControllerException("follower_id必须为null");
} else if (follow.getFollowing_id() == null) {
throw new ControllerException("following_id不可为null");
} else {
follow.setFollower_id(userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
.getId());
if (follow.getFollower_id() == follow.getFollowing_id()) {
throw new ControllerException("不可自己关注自己");
} else if (followService.selectByFollower_idFollowing_id(follow.getFollower_id(),
follow.getFollowing_id()) != null) {
throw new ControllerException("该用户已经关注过了,不可重复关注");
} else {
return new ResponseObject("200", "操作成功", followService.insert(follow));
}
}
}
@GetMapping
@ApiOperation("查询Follow")
public ResponseObject get(Integer follower_id, Integer following_id) {
log.info("查询Follow");
if (follower_id != null && following_id != null) {
throw new ControllerException("novel_id不可为null");
} else {
Integer novel_id = Integer.parseInt(novel_idString);
Novel novel = novelService.selectById(novel_id);
if (novel == null) {
throw new ControllerException("根据novel_id查询出来的novel为null");
} else {
User user = userService.selectByUsername(
(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
if (user.getId() == novel.getUser_id() || user.getRole().equals("ADMIN")) {
novel.setImage(image);
return new ResponseObject("200", "操作成功", novelService.update(novel));
} else {
throw new ControllerException("该用户无权限修改小说头像");
}
}
}
}
}
@RestController
public ResponseObject delete(Integer following_id) {
log.info("删除Follow");
if (following_id == null) {
throw new ControllerException("following_id不可为null");
} else {
Integer follower_id = userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
.getId();
Follow follow = followService.selectByFollower_idFollowing_id(follower_id, following_id);
if (follow == null) {
throw new ControllerException("该用户未关注,无法取消关注");
} else {
followService.deleteById(follow.getId());
return new ResponseObject("200", "操作成功", null);
}
}
}
}
@RestController
@RequestMapping("/api/category")
@Api(tags = "共同前缀:/api/category", description = "CategoryController")
@Slf4j
public class CategoryController {
@Autowired
CategoryService categoryService;
@PostMapping
@ApiOperation("新增Category")
throw new ControllerException("novel_id不可为null");
}
}
@GetMapping("/{id:[0-9]+}")
@ApiOperation("查询Volume")
public ResponseObject getById(@PathVariable Integer id) {
log.info("查询Volume");
if (id != null) {
return new ResponseObject("200", "操作成功", volumeService.selectById(id));
} else {
throw new ControllerException("id不可为null");
}
}
@PatchMapping("/{id:[0-9]+}")
@ApiOperation("修改Volume")
@PreAuthorize("isAuthenticated()")
public ResponseObject patchById(@PathVariable Integer id,@RequestBody Volume volume) {
log.info("修改Volume");
if (id == null) {
throw new ControllerException("id不可为null");
} else {
Volume volume2 = volumeService.selectById(id);
if (volume2 == null) {
throw new ControllerException("不存在为该id的volume");
} else if (volume.getName() != null && !volume.getName().equals("")) {
volume2.setName(volume.getName());
if (volumeService.selectByNovel_idName(volume2.getNovel_id(), volume2.getName()) != null) {
throw new ControllerException("name不可重复");
}
} else if (volume.getSummary() != null && !volume.getSummary().equals("")) {
volume2.setSummary(volume.getSummary());
} else {
throw new ControllerException("请传入需要修改的数据,如name,summary");
}
User user = userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
if (user.getId() == novelService.selectById(volume2.getNovel_id()).getUser_id()
|| user.getRole().equals("ADMIN")) {
return new ResponseObject("200", "操作成功", volumeService.update(volume2));
@Slf4j
public class FollowController {
@Autowired
UserService userService;
@Autowired
FollowService followService;
@PostMapping
@ApiOperation("新增Follow")
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody Follow follow) {
log.info("新增Follow");
System.out.println(follow);
if (follow.getId() != null) {
throw new ControllerException("id必须为null");
} else if (follow.getFollower_id() != null) {
throw new ControllerException("follower_id必须为null");
} else if (follow.getFollowing_id() == null) {
throw new ControllerException("following_id不可为null");
} else {
follow.setFollower_id(userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
.getId());
if (follow.getFollower_id() == follow.getFollowing_id()) {
throw new ControllerException("不可自己关注自己");
} else if (followService.selectByFollower_idFollowing_id(follow.getFollower_id(),
follow.getFollowing_id()) != null) {
throw new ControllerException("该用户已经关注过了,不可重复关注");
} else {
return new ResponseObject("200", "操作成功", followService.insert(follow));
}
}
}
@GetMapping
@ApiOperation("查询Follow")
public ResponseObject get(Integer follower_id, Integer following_id) {
log.info("查询Follow");
if (follower_id != null && following_id != null) {
return new ResponseObject("200", "操作成功",
followService.selectByFollower_idFollowing_id(follower_id, following_id));
} else if (follower_id != null && following_id == null) {
return new ResponseObject("200", "操作成功", userService.selectByFollower_id(follower_id));
} else if (follower_id == null && following_id != null) {
return new ResponseObject("200", "操作成功", userService.selectByFollowing_id(following_id));
@Autowired
ObjectMapper objectMapper;
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException accessDeniedException) throws IOException, ServletException {
log.info("已认证无权限,返回JSON格式的异常信息");
response.setStatus(403);
response.setCharacterEncoding("utf-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter writer = response.getWriter();
writer.write(objectMapper.writeValueAsString(new ResponseObject("403", "已认证无权限", null)));
writer.flush();
writer.close();
}
}
//未认证异常处理器
@Component
@Slf4j
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Autowired
ObjectMapper objectMapper;
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
@PreAuthorize("isAuthenticated()")
public ResponseObject patchContent(String content, @RequestBody HashMap<String, String> data) {
log.info("修改Chapter的content");
if (content == null || content.equals("")) {
throw new ControllerException("content不可为null,也不可为空字符串");
} else if (data.get("chapterContent") == null || data.get("chapterContent").equals("")) {
throw new ControllerException("chapterContent不可为null,也不可为空字符串");
} else {
try {
File file = new File(new File(ResourceUtils.getURL("src").getPath() + "main/resources/static/txt/"),
content);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(data.get("chapterContent"));
fileWriter.flush();
fileWriter.close();
return new ResponseObject("200", "操作成功", data.get("chapterContent"));
} catch (Exception e) {
throw new ControllerException("找不到该章节的内容");
}
}
}
}
@RestController
@RequestMapping("/api/volume")
@Api(tags = "共同前缀:/api/volume", description = "VolumeController")
@Slf4j
@RestController
@RequestMapping("/api/novel")
@Api(tags = "共同前缀:/api/novel", description = "NovelController")
@Slf4j
public class NovelController {
@Autowired
NovelService novelService;
@Autowired
UserService userService;
@PostMapping
@ApiOperation("新增Novel")
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody Novel novel) {
log.info("新增Novel");
if (novel.getId() != null) {
throw new ControllerException("id必须为null");
} else if (novel.getUser_id() == null) {
throw new ControllerException("user_id不可为null");
} else if (novel.getCategory_id() == null) {
throw new ControllerException("category_id不可为null");
} else if (novel.getName() == null || novel.getName().equals("")) {
throw new ControllerException("name不可为null,也不可为空字符串");
} else if (novel.getSummary() == null || novel.getSummary().equals("")) {
throw new ControllerException("summary不可为null,也不可为空字符串");
} else if (novel.getImage() == null || novel.getImage().equals("")) {
@RestController
@RequestMapping("/api")
@Api(tags = "共同前缀:/api", description = "AppController")
@Slf4j
public class AppController {
@Autowired
UserService userService;
@Autowired
NovelService novelService;
@GetMapping("/me")
@ApiOperation("查询Me")
@PreAuthorize("isAuthenticated()")
public ResponseObject getMe() {
log.info("查询Me");
return new ResponseObject("200", "操作成功", userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal()));
}
}
//全局异常处理类
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(value = { ControllerException.class, ServiceException.class })
public ResponseEntity<ResponseObject> controllerExceptionHandler(Exception e) {
@RestController
@RequestMapping("/api/novel")
@Api(tags = "共同前缀:/api/novel", description = "NovelController")
@Slf4j
public class NovelController {
@Autowired
NovelService novelService;
@Autowired
UserService userService;
@PostMapping
@ApiOperation("新增Novel")
@PreAuthorize("isAuthenticated()")
@RestController
@RequestMapping("/api/chapter")
@Api(tags = "共同前缀:/api/chapter", description = "ChapterController")
@Slf4j
public class ChapterController {
@Autowired
ChapterService chapterService;
@PostMapping
@ApiOperation("新增Chapter")
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody HashMap<String, String> data) {
log.info("新增Chapter");
if (data.get("title") == null || data.get("title").equals("")) {
throw new ControllerException("volume_id不可为null");
} else if (data.get("volume_id") == null || data.get("volume_id").equals("")) {
throw new ControllerException("title不可为null,也不可为空字符串");
} else if (data.get("chapterContent") == null || data.get("chapterContent").equals("")) {
throw new ControllerException("chapterContent不可为null,也不可为空字符串");
} else {
try {
// 文件夹
File directory = new File(ResourceUtils.getURL("src").getPath() + "main/resources/static/txt/");
if (!directory.exists()) {
directory.mkdirs();
}
// 文件
@Slf4j
public class FollowController {
@Autowired
UserService userService;
@Autowired
FollowService followService;
@PostMapping
@ApiOperation("新增Follow")
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody Follow follow) {
log.info("新增Follow");
System.out.println(follow);
if (follow.getId() != null) {
throw new ControllerException("id必须为null");
} else if (follow.getFollower_id() != null) {
throw new ControllerException("follower_id必须为null");
} else if (follow.getFollowing_id() == null) {
throw new ControllerException("following_id不可为null");
} else {
follow.setFollower_id(userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
.getId());
if (follow.getFollower_id() == follow.getFollowing_id()) {
throw new ControllerException("不可自己关注自己");
} else if (followService.selectByFollower_idFollowing_id(follow.getFollower_id(),
follow.getFollowing_id()) != null) {
throw new ControllerException("该用户已经关注过了,不可重复关注");
} else {
return new ResponseObject("200", "操作成功", followService.insert(follow));
}
}
}
@GetMapping
@ApiOperation("查询Follow")
public ResponseObject get(Integer follower_id, Integer following_id) {
log.info("查询Follow");
if (follower_id != null && following_id != null) {
return new ResponseObject("200", "操作成功",
@RestController
@RequestMapping("/api/category")
@Api(tags = "共同前缀:/api/category", description = "CategoryController")
@Slf4j
public class CategoryController {
@Autowired
CategoryService categoryService;
@PostMapping
@ApiOperation("新增Category")
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN')")
public ResponseObject post(@RequestBody Category category) {
log.info("新增Category");
if (category.getId() != null) {
throw new ControllerException("id必须为null");
} else if (category.getRank() == null) {
throw new ControllerException("rank不可为null");
} else if (category.getName() == null || category.getName().equals("")) {
throw new ControllerException("name不可为null,也不可为空字符串");
} else if (category.getParent_id() == null) {
throw new ControllerException("parent_id不可为null");
} else if (categoryService.selectByParent_idName(category.getParent_id(), category.getName()) != null) {
throw new ControllerException("name不可重复");
} else {
return new ResponseObject("200", "操作成功", categoryService.insert(category));
}
}
@PutMapping("/{id:[0-9]+}")
@ApiOperation("修改Category")
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN')")
public ResponseObject putById(@PathVariable Integer id, @RequestBody HashMap<String, String> data) {
log.info("修改Category");
Category category = categoryService.selectById(id);
if (category == null) {
throw new ControllerException("使用该id的Category不存在");
@GetMapping
@ApiOperation("查询Comment")
public ResponseObject get(Integer novel_id, Integer user_id) {
log.info("查询Comment");
if (novel_id != null && user_id != null) {
return new ResponseObject("200", "操作成功", commentService.selectByUser_idNovel_id(user_id, novel_id));
} else if (novel_id != null && user_id == null) {
return new ResponseObject("200", "操作成功", commentService.selectByNovel_id(novel_id));
} else if (novel_id == null && user_id != null) {
return new ResponseObject("200", "操作成功", commentService.selectByUser_id(user_id));
} else {
throw new ControllerException("novel_id和user_id不可同时为null");
}
}
@GetMapping("/count")
@ApiOperation("查询Comment")
public ResponseObject getCount(Integer novel_id) {
log.info("查询Comment");
if (novel_id == null) {
throw new ControllerException("novel_id不可为null");
} else {
return new ResponseObject("200", "操作成功", commentService.selectByNovel_id(novel_id).size());
}
}
}
//无权限异常处理器
@Component
@Slf4j
public class MyAccessDeniedHandler implements AccessDeniedHandler {
@Autowired
ObjectMapper objectMapper;
@RequestMapping("/api/user")
@Api(tags = "共同前缀:/api/user", description = "UserController")
@Slf4j
public class UserController {
@Autowired
UserService userService;
@PostMapping
@ApiOperation("新增User")
public ResponseObject post(String username, String password) {
log.info("新增User");
if (username == null || username.equals("")) {
throw new ControllerException("username不可为null,也不可为空字符串");
} else if (password == null || password.equals("")) {
throw new ControllerException("password不可为null,也不可为空字符串");
} else if (userService.selectByUsername(username) != null) {
throw new ControllerException("该username已被使用");
} else {
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setRole("VIP1");
user.setNickname(new Date().getTime() + "");
user.setImage("default_user_image.png");
user.setEmail("该用户没有填写邮箱");
user.setPhone("该用户没有填写手机号码");
user.setProfile("该用户没有填写个人简介");
return new ResponseObject("200", "操作成功", userService.insert(user));
}
}
@GetMapping("/{id:[0-9]+}")
@ApiOperation("查询User")
public ResponseObject getById(@PathVariable Integer id) {
log.info("查询User");
return new ResponseObject("200", "操作成功", userService.selectById(id));
}
@PutMapping
@ApiOperation("修改User")
@PreAuthorize("isAuthenticated()")
public ResponseObject put(User user) {
log.info("修改User");
if (user.getNickname() == null || user.getNickname().equals("")) {
throw new ControllerException("nickname不可为null,也不可为空字符串");
} else if (user.getProfile() == null || user.getProfile().equals("")) {
throw new ControllerException("profile不可为null,也不可为空字符串");
} else if (user.getPhone() == null || user.getPhone().equals("")) {
public ResponseObject getNext(@PathVariable Integer id) {
log.info("查询下一章的Chapter");
if (id == null) {
throw new ControllerException("id不可为null");
} else {
Chapter chapter = chapterService.selectById(id);
if (chapter == null) {
throw new ControllerException("该id无法查找到chapter");
} else {
return new ResponseObject("200", "操作成功",
chapterService.selectNextByVolume_idId(chapter.getVolume_id(), chapter.getId()));
}
}
}
@GetMapping("/{id:[0-9]+}")
@ApiOperation("查询Chapter")
public ResponseObject getById(@PathVariable Integer id) {
log.info("查询Chapter");
if (id == null) {
throw new ControllerException("id不可为null");
} else {
return new ResponseObject("200", "操作成功", chapterService.selectById(id));
}
}
@GetMapping("/content")
@ApiOperation("查询Chapter的content")
public ResponseObject getContent(String content) {
log.info("查询Chapter的content");
if (content == null || content.equals("")) {
throw new ControllerException("content不可为null,也不可为空字符串");
} else {
try {
StringBuilder stringBuilder = new StringBuilder();
File file = new File(new File(ResourceUtils.getURL("src").getPath() + "main/resources/static/txt/"),
content);
FileReader fileReader = new FileReader(file);
char[] charArray = new char[1024];
int length = 0;
while ((length = fileReader.read(charArray)) != -1) {
stringBuilder.append(new String(charArray, 0, length));
}
fileReader.close();
}
@GetMapping
@ApiOperation("查询Comment")
public ResponseObject get(Integer novel_id, Integer user_id) {
log.info("查询Comment");
if (novel_id != null && user_id != null) {
return new ResponseObject("200", "操作成功", commentService.selectByUser_idNovel_id(user_id, novel_id));
} else if (novel_id != null && user_id == null) {
return new ResponseObject("200", "操作成功", commentService.selectByNovel_id(novel_id));
} else if (novel_id == null && user_id != null) {
return new ResponseObject("200", "操作成功", commentService.selectByUser_id(user_id));
} else {
throw new ControllerException("novel_id和user_id不可同时为null");
}
}
@GetMapping("/count")
@ApiOperation("查询Comment")
public ResponseObject getCount(Integer novel_id) {
log.info("查询Comment");
if (novel_id == null) {
throw new ControllerException("novel_id不可为null");
} else {
return new ResponseObject("200", "操作成功", commentService.selectByNovel_id(novel_id).size());
}
}
}
//无权限异常处理器
@Component
@Slf4j
public class MyAccessDeniedHandler implements AccessDeniedHandler {
@Autowired