refactor: 使用 Hutool工具类优化空值判断- 在多个控制器和服务实现类中,将原始的空值判断替换为 Hutool 工具类中的方法
- 使用 ObjectUtil.isNull() 替换原始的 == null 判断 - 使用 ObjectUtil.isNotNull() 替换原始的 != null 判断 - 在某些情况下,使用 StrUtil.isBlank() 替换原始的 == null 判断,以更准确地检查字符串是否为空
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.test.bijihoudaun.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.test.bijihoudaun.common.response.R;
|
||||
import com.test.bijihoudaun.entity.Grouping;
|
||||
import com.test.bijihoudaun.service.GroupingService;
|
||||
@@ -24,7 +25,7 @@ public class GroupingController {
|
||||
@Operation(summary = "创建分组")
|
||||
@PostMapping
|
||||
public R<Grouping> createGrouping(@RequestBody Grouping grouping) {
|
||||
if (grouping.getParentId() == null) {
|
||||
if (ObjectUtil.isNull(grouping.getParentId())) {
|
||||
grouping.setParentId(0L);
|
||||
}
|
||||
Grouping created = groupingService.createGrouping(grouping);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.test.bijihoudaun.controller;
|
||||
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.test.bijihoudaun.common.response.R;
|
||||
import com.test.bijihoudaun.entity.Image;
|
||||
import com.test.bijihoudaun.service.ImageService;
|
||||
@@ -63,7 +64,7 @@ public class ImageController {
|
||||
@GetMapping("/preview/{url}")
|
||||
@Operation(summary = "在线预览", description = "浏览器直接打开文件流")
|
||||
public void preview(@PathVariable String url, HttpServletResponse resp) throws IOException {
|
||||
if (url == null) {
|
||||
if (StrUtil.isBlank(url)) {
|
||||
resp.setStatus(404);
|
||||
R.fail("文件不存在");
|
||||
}
|
||||
@@ -113,7 +114,7 @@ public class ImageController {
|
||||
* @return 对应的MIME类型
|
||||
*/
|
||||
private String getContentTypeFromFileExtension(String fileName) {
|
||||
if (fileName == null || fileName.lastIndexOf('.') == -1) {
|
||||
if (StrUtil.isBlank(fileName) || !StrUtil.contains(fileName, '.')) {
|
||||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.test.bijihoudaun.controller;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.test.bijihoudaun.common.response.R;
|
||||
import com.test.bijihoudaun.entity.MarkdownFile;
|
||||
import com.test.bijihoudaun.entity.MarkdownFileVO;
|
||||
@@ -36,7 +37,7 @@ public class MarkdownController {
|
||||
@GetMapping("/{id}")
|
||||
public R<String> getMarkdownContent(@PathVariable Long id) {
|
||||
MarkdownFile file = markdownFileService.getMarkdownById(id);
|
||||
if (file != null) {
|
||||
if (ObjectUtil.isNotNull(file)) {
|
||||
return R.success(file.getContent());
|
||||
}
|
||||
return R.fail();
|
||||
@@ -89,7 +90,7 @@ public class MarkdownController {
|
||||
@PathVariable Long id,
|
||||
@RequestBody String title) {
|
||||
MarkdownFile updatedFile = markdownFileService.updateMarkdownTitle(id, title);
|
||||
if (updatedFile != null) {
|
||||
if (ObjectUtil.isNotNull(updatedFile)) {
|
||||
return R.success(updatedFile);
|
||||
}
|
||||
return R.fail("文件未找到或更新失败");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.test.bijihoudaun.controller;
|
||||
|
||||
import com.test.bijihoudaun.bo.UpdatePasswordBo;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.test.bijihoudaun.common.response.R;
|
||||
import com.test.bijihoudaun.entity.User;
|
||||
import com.test.bijihoudaun.service.RegistrationCodeService;
|
||||
@@ -69,7 +70,7 @@ public class UserController {
|
||||
String username = userDetails.getUsername();
|
||||
User user = userService.getOne(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<User>().eq("username", username));
|
||||
|
||||
if (user == null) {
|
||||
if (ObjectUtil.isNull(user)) {
|
||||
return R.fail("无法获取用户信息,删除失败");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user