区域数据序列化与反序列化bug修复

This commit is contained in:
attiya 2025-10-22 10:23:13 +08:00
parent 53eaa2167a
commit 22d64a6111
2 changed files with 16 additions and 53 deletions

View File

@ -9,6 +9,7 @@ import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.StringUtil;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@ -23,6 +24,7 @@ import static com.cdzy.operations.model.entity.table.EbikeRegionTableDef.EBIKE_R
*/
@RestController
@RequestMapping("/ebikeRegion")
@Validated
public class EbikeRegionController {
@Resource
@ -35,7 +37,7 @@ public class EbikeRegionController {
* @return {@code true} 添加成功{@code false} 添加失败
*/
@PostMapping("save")
public JsonResult<?> save(@RequestBody EbikeRegionVo ebikeRegion) {
public JsonResult<?> save(@Validated @RequestBody EbikeRegionVo ebikeRegion) {
ebikeRegionService.save(ebikeRegion);
return JsonResult.success();
}
@ -47,7 +49,7 @@ public class EbikeRegionController {
* @return {@code true} 删除成功{@code false} 删除失败
*/
@GetMapping("remove")
public JsonResult<?> remove(@RequestParam Long regionId) {
public JsonResult<?> remove(@Validated @RequestParam Long regionId) {
ebikeRegionService.removeById(regionId);
return JsonResult.success();
}

View File

@ -25,28 +25,24 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
@Override
public void serialize(PGpolygon polygon, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (polygon == null) {
System.out.println("PGpolygonSerializer: 输入多边形为 null写入 null");
gen.writeNull();
return;
}
System.out.println("=== PGpolygonSerializer 开始序列化 ===");
try {
// 解析多边形点坐标
PGpoint[] points = parsePointsFromPolygon(polygon);
System.out.println("成功解析到 " + points.length + " 个点");
if (points.length == 0) {
System.err.println("警告: 没有解析到任何有效点坐标");
log.error("警告: 没有解析到任何有效点坐标");
}
// 检查是否有 null
for (int i = 0; i < points.length; i++) {
if (points[i] == null) {
System.err.println("错误: 第 " + i + " 个点为 null");
log.error("错误: 第 {} 个点为 null", i);
} else {
System.out.println("" + i + ": (" + points[i].x + ", " + points[i].y + ")");
log.error("点 {}: ({}, {})", i, points[i].x, points[i].y);
}
}
@ -58,7 +54,6 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
for (int i = 0; i < points.length; i++) {
PGpoint point = points[i];
if (point == null) {
System.err.println("序列化时跳过第 " + i + " 个 null 点");
continue;
}
@ -67,17 +62,13 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
gen.writeNumber(point.x);
gen.writeNumber(point.y);
gen.writeEndArray();
System.out.println("成功序列化点 " + i + ": (" + point.x + ", " + point.y + ")");
} catch (Exception e) {
System.err.println("序列化点 " + i + " 时出错: " + e.getMessage());
throw new IOException("序列化点 " + i + " 失败", e);
}
}
gen.writeEndArray();
gen.writeEndObject();
System.out.println("=== PGpolygonSerializer 序列化完成 ===");
} catch (Exception e) {
log.error("!!! PGpolygonSerializer 序列化失败 !!!");
log.error("错误信息: {}", e.getMessage());
@ -99,29 +90,21 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
throw new IOException("多边形对象为 null");
}
System.out.println("开始解析多边形...");
try {
// 方法1: 尝试使用反射获取 points 数组
System.out.println("尝试通过反射获取点坐标...");
PGpoint[] points = tryGetPointsByReflection(polygon);
if (points != null && points.length > 0) {
System.out.println("通过反射获取到 " + points.length + " 个点");
return filterNullPoints(points);
}
// 方法2: 字符串解析
System.out.println("反射失败,尝试字符串解析...");
String polygonString = polygon.toString();
System.out.println("多边形字符串: " + polygonString);
points = parsePointsFromString(polygonString);
System.out.println("通过字符串解析获取到 " + points.length + " 个点");
return filterNullPoints(points);
} catch (Exception e) {
System.err.println("解析多边形点坐标时发生异常: " + e.getMessage());
throw new IOException("解析多边形点坐标失败: " + e.getMessage(), e);
}
}
@ -131,7 +114,6 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
*/
private PGpoint[] tryGetPointsByReflection(PGpolygon polygon) {
try {
System.out.println("使用反射访问 points 字段...");
Field pointsField = polygon.getClass().getDeclaredField("points");
pointsField.setAccessible(true);
Object pointsObj = pointsField.get(polygon);
@ -140,14 +122,14 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
log.info("反射成功,获取到 {} 个点", points.length);
return points;
} else {
System.err.println("反射获取的 points 字段类型不正确: " + (pointsObj != null ? pointsObj.getClass() : "null"));
log.error("反射获取的 points 字段类型不正确: {}", pointsObj != null ? pointsObj.getClass() : "null");
}
} catch (NoSuchFieldException e) {
System.err.println("反射失败: PGpolygon 类中没有 points 字段");
log.error("反射失败: PGpolygon 类中没有 points 字段");
} catch (IllegalAccessException e) {
System.err.println("反射失败: 无法访问 points 字段 - " + e.getMessage());
log.error("反射失败: 无法访问 points 字段 - " + e.getMessage());
} catch (Exception e) {
System.err.println("反射失败: " + e.getMessage());
log.error("反射失败: " + e.getMessage());
}
return null;
}
@ -162,11 +144,8 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
throw new IOException("多边形字符串为空");
}
System.out.println("开始解析字符串: " + polygonString);
try {
// 方法1: 正则表达式匹配所有数字对
System.out.println("使用正则表达式匹配坐标...");
Matcher matcher = COORD_PATTERN.matcher(polygonString);
int matchCount = 0;
@ -176,9 +155,7 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
double y = Double.parseDouble(matcher.group(2));
pointsList.add(new PGpoint(x, y));
matchCount++;
System.out.println("正则匹配到点 " + matchCount + ": (" + x + ", " + y + ")");
} catch (NumberFormatException e) {
System.err.println("坐标格式错误: " + matcher.group());
throw new IOException("坐标格式错误: " + matcher.group(), e);
}
}
@ -187,9 +164,7 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
// 方法2: 如果正则没有匹配到尝试手动解析
if (pointsList.isEmpty()) {
System.out.println("正则匹配失败,尝试手动解析...");
pointsList = parsePointsManually(polygonString);
System.out.println("手动解析到 " + pointsList.size() + " 个点");
}
if (pointsList.isEmpty()) {
@ -197,7 +172,6 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
}
} catch (Exception e) {
System.err.println("字符串解析过程中发生异常: " + e.getMessage());
throw new IOException("字符串解析失败: " + e.getMessage(), e);
}
@ -210,12 +184,9 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
private List<PGpoint> parsePointsManually(String polygonString) throws IOException {
List<PGpoint> pointsList = new ArrayList<>();
System.out.println("开始手动解析...");
try {
// 移除所有括号
String cleaned = polygonString.replaceAll("[()]", "").trim();
System.out.println("清理后字符串: " + cleaned);
if (cleaned.isEmpty()) {
throw new IOException("清理后字符串为空");
@ -223,10 +194,9 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
// 分割点对
String[] pointPairs = cleaned.split(",");
System.out.println("分割出 " + pointPairs.length + " 个点对");
for (int i = 0; i < pointPairs.length; i++) {
String pointPair = pointPairs[i].trim();
for (String pair : pointPairs) {
String pointPair = pair.trim();
if (pointPair.isEmpty()) {
continue;
}
@ -242,21 +212,16 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
}
}
System.out.println("点对 " + i + " 有效坐标数: " + validCoords.size());
if (validCoords.size() >= 2) {
try {
double x = Double.parseDouble(validCoords.get(0));
double y = Double.parseDouble(validCoords.get(1));
PGpoint point = new PGpoint(x, y);
pointsList.add(point);
System.out.println("手动解析点 " + i + ": (" + x + ", " + y + ")");
} catch (NumberFormatException e) {
System.err.println("坐标解析失败 - 点对 " + i + ": " + pointPair);
throw new IOException("坐标解析失败: " + pointPair, e);
}
} else {
System.err.println("坐标对格式不正确 - 点对 " + i + ": " + pointPair);
throw new IOException("坐标对格式不正确: " + pointPair);
}
}
@ -266,7 +231,6 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
}
} catch (Exception e) {
System.err.println("手动解析过程中发生异常: " + e.getMessage());
throw new IOException("手动解析失败: " + e.getMessage(), e);
}
@ -284,17 +248,14 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
List<PGpoint> filtered = new ArrayList<>();
int nullCount = 0;
for (int i = 0; i < points.length; i++) {
if (points[i] != null) {
filtered.add(points[i]);
for (PGpoint point : points) {
if (point != null) {
filtered.add(point);
} else {
nullCount++;
System.err.println("发现第 " + i + " 个点为 null");
}
}
System.out.println("过滤结果: " + filtered.size() + " 个有效点, " + nullCount + " 个 null 点");
if (filtered.isEmpty()) {
throw new IOException("过滤后没有剩余有效点");
}