11
This commit is contained in:
43
docs/add_json_format_annotations.sh
Normal file
43
docs/add_json_format_annotations.sh
Normal file
@@ -0,0 +1,43 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "=== 为LocalDateTime字段添加@JsonFormat注解 ==="
|
||||
echo
|
||||
|
||||
# 获取所有包含LocalDateTime的实体类文件
|
||||
files=$(find src/main/java -path "*/entity/*" -name "*.java" -exec grep -l "LocalDateTime" {} \;)
|
||||
|
||||
for file in $files; do
|
||||
echo "处理文件: $file"
|
||||
|
||||
# 检查是否已经导入JsonFormat
|
||||
if ! grep -q "import com.fasterxml.jackson.annotation.JsonFormat" "$file"; then
|
||||
echo " 添加JsonFormat导入..."
|
||||
# 在LocalDateTime导入后添加JsonFormat导入
|
||||
sed -i '' '/import java\.time\.LocalDateTime;/a\
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
' "$file"
|
||||
fi
|
||||
|
||||
# 为LocalDateTime字段添加@JsonFormat注解
|
||||
echo " 添加@JsonFormat注解..."
|
||||
|
||||
# 处理各种时间字段模式
|
||||
sed -i '' '/private LocalDateTime.*Time;/i\
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
' "$file"
|
||||
|
||||
# 移除重复的注解(如果存在)
|
||||
awk '
|
||||
/^[[:space:]]*@JsonFormat\(pattern = "yyyy-MM-dd HH:mm:ss"\)/ {
|
||||
if (prev_line == $0) next
|
||||
prev_line = $0
|
||||
}
|
||||
{ print }
|
||||
' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
|
||||
|
||||
echo " 完成处理: $file"
|
||||
done
|
||||
|
||||
echo
|
||||
echo "=== 批量添加@JsonFormat注解完成 ==="
|
||||
echo "请重启应用程序测试效果"
|
||||
Reference in New Issue
Block a user