区域数据序列化与反序列化bug修复
This commit is contained in:
parent
53eaa2167a
commit
22d64a6111
@ -9,6 +9,7 @@ import com.mybatisflex.core.paginate.Page;
|
|||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -23,6 +24,7 @@ import static com.cdzy.operations.model.entity.table.EbikeRegionTableDef.EBIKE_R
|
|||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/ebikeRegion")
|
@RequestMapping("/ebikeRegion")
|
||||||
|
@Validated
|
||||||
public class EbikeRegionController {
|
public class EbikeRegionController {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@ -35,7 +37,7 @@ public class EbikeRegionController {
|
|||||||
* @return {@code true} 添加成功,{@code false} 添加失败
|
* @return {@code true} 添加成功,{@code false} 添加失败
|
||||||
*/
|
*/
|
||||||
@PostMapping("save")
|
@PostMapping("save")
|
||||||
public JsonResult<?> save(@RequestBody EbikeRegionVo ebikeRegion) {
|
public JsonResult<?> save(@Validated @RequestBody EbikeRegionVo ebikeRegion) {
|
||||||
ebikeRegionService.save(ebikeRegion);
|
ebikeRegionService.save(ebikeRegion);
|
||||||
return JsonResult.success();
|
return JsonResult.success();
|
||||||
}
|
}
|
||||||
@ -47,7 +49,7 @@ public class EbikeRegionController {
|
|||||||
* @return {@code true} 删除成功,{@code false} 删除失败
|
* @return {@code true} 删除成功,{@code false} 删除失败
|
||||||
*/
|
*/
|
||||||
@GetMapping("remove")
|
@GetMapping("remove")
|
||||||
public JsonResult<?> remove(@RequestParam Long regionId) {
|
public JsonResult<?> remove(@Validated @RequestParam Long regionId) {
|
||||||
ebikeRegionService.removeById(regionId);
|
ebikeRegionService.removeById(regionId);
|
||||||
return JsonResult.success();
|
return JsonResult.success();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,28 +25,24 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
@Override
|
@Override
|
||||||
public void serialize(PGpolygon polygon, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
public void serialize(PGpolygon polygon, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||||
if (polygon == null) {
|
if (polygon == null) {
|
||||||
System.out.println("PGpolygonSerializer: 输入多边形为 null,写入 null");
|
|
||||||
gen.writeNull();
|
gen.writeNull();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("=== PGpolygonSerializer 开始序列化 ===");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 解析多边形点坐标
|
// 解析多边形点坐标
|
||||||
PGpoint[] points = parsePointsFromPolygon(polygon);
|
PGpoint[] points = parsePointsFromPolygon(polygon);
|
||||||
System.out.println("成功解析到 " + points.length + " 个点");
|
|
||||||
|
|
||||||
if (points.length == 0) {
|
if (points.length == 0) {
|
||||||
System.err.println("警告: 没有解析到任何有效点坐标");
|
log.error("警告: 没有解析到任何有效点坐标");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否有 null 点
|
// 检查是否有 null 点
|
||||||
for (int i = 0; i < points.length; i++) {
|
for (int i = 0; i < points.length; i++) {
|
||||||
if (points[i] == null) {
|
if (points[i] == null) {
|
||||||
System.err.println("错误: 第 " + i + " 个点为 null");
|
log.error("错误: 第 {} 个点为 null", i);
|
||||||
} else {
|
} 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++) {
|
for (int i = 0; i < points.length; i++) {
|
||||||
PGpoint point = points[i];
|
PGpoint point = points[i];
|
||||||
if (point == null) {
|
if (point == null) {
|
||||||
System.err.println("序列化时跳过第 " + i + " 个 null 点");
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,17 +62,13 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
gen.writeNumber(point.x);
|
gen.writeNumber(point.x);
|
||||||
gen.writeNumber(point.y);
|
gen.writeNumber(point.y);
|
||||||
gen.writeEndArray();
|
gen.writeEndArray();
|
||||||
System.out.println("成功序列化点 " + i + ": (" + point.x + ", " + point.y + ")");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("序列化点 " + i + " 时出错: " + e.getMessage());
|
|
||||||
throw new IOException("序列化点 " + i + " 失败", e);
|
throw new IOException("序列化点 " + i + " 失败", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gen.writeEndArray();
|
gen.writeEndArray();
|
||||||
gen.writeEndObject();
|
gen.writeEndObject();
|
||||||
|
|
||||||
System.out.println("=== PGpolygonSerializer 序列化完成 ===");
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("!!! PGpolygonSerializer 序列化失败 !!!");
|
log.error("!!! PGpolygonSerializer 序列化失败 !!!");
|
||||||
log.error("错误信息: {}", e.getMessage());
|
log.error("错误信息: {}", e.getMessage());
|
||||||
@ -99,29 +90,21 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
throw new IOException("多边形对象为 null");
|
throw new IOException("多边形对象为 null");
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("开始解析多边形...");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 方法1: 尝试使用反射获取 points 数组
|
// 方法1: 尝试使用反射获取 points 数组
|
||||||
System.out.println("尝试通过反射获取点坐标...");
|
|
||||||
PGpoint[] points = tryGetPointsByReflection(polygon);
|
PGpoint[] points = tryGetPointsByReflection(polygon);
|
||||||
if (points != null && points.length > 0) {
|
if (points != null && points.length > 0) {
|
||||||
System.out.println("通过反射获取到 " + points.length + " 个点");
|
|
||||||
return filterNullPoints(points);
|
return filterNullPoints(points);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 方法2: 字符串解析
|
// 方法2: 字符串解析
|
||||||
System.out.println("反射失败,尝试字符串解析...");
|
|
||||||
String polygonString = polygon.toString();
|
String polygonString = polygon.toString();
|
||||||
System.out.println("多边形字符串: " + polygonString);
|
|
||||||
|
|
||||||
points = parsePointsFromString(polygonString);
|
points = parsePointsFromString(polygonString);
|
||||||
System.out.println("通过字符串解析获取到 " + points.length + " 个点");
|
|
||||||
|
|
||||||
return filterNullPoints(points);
|
return filterNullPoints(points);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("解析多边形点坐标时发生异常: " + e.getMessage());
|
|
||||||
throw new IOException("解析多边形点坐标失败: " + e.getMessage(), e);
|
throw new IOException("解析多边形点坐标失败: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,7 +114,6 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
*/
|
*/
|
||||||
private PGpoint[] tryGetPointsByReflection(PGpolygon polygon) {
|
private PGpoint[] tryGetPointsByReflection(PGpolygon polygon) {
|
||||||
try {
|
try {
|
||||||
System.out.println("使用反射访问 points 字段...");
|
|
||||||
Field pointsField = polygon.getClass().getDeclaredField("points");
|
Field pointsField = polygon.getClass().getDeclaredField("points");
|
||||||
pointsField.setAccessible(true);
|
pointsField.setAccessible(true);
|
||||||
Object pointsObj = pointsField.get(polygon);
|
Object pointsObj = pointsField.get(polygon);
|
||||||
@ -140,14 +122,14 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
log.info("反射成功,获取到 {} 个点", points.length);
|
log.info("反射成功,获取到 {} 个点", points.length);
|
||||||
return points;
|
return points;
|
||||||
} else {
|
} else {
|
||||||
System.err.println("反射获取的 points 字段类型不正确: " + (pointsObj != null ? pointsObj.getClass() : "null"));
|
log.error("反射获取的 points 字段类型不正确: {}", pointsObj != null ? pointsObj.getClass() : "null");
|
||||||
}
|
}
|
||||||
} catch (NoSuchFieldException e) {
|
} catch (NoSuchFieldException e) {
|
||||||
System.err.println("反射失败: PGpolygon 类中没有 points 字段");
|
log.error("反射失败: PGpolygon 类中没有 points 字段");
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
System.err.println("反射失败: 无法访问 points 字段 - " + e.getMessage());
|
log.error("反射失败: 无法访问 points 字段 - " + e.getMessage());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("反射失败: " + e.getMessage());
|
log.error("反射失败: " + e.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -162,11 +144,8 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
throw new IOException("多边形字符串为空");
|
throw new IOException("多边形字符串为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("开始解析字符串: " + polygonString);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 方法1: 正则表达式匹配所有数字对
|
// 方法1: 正则表达式匹配所有数字对
|
||||||
System.out.println("使用正则表达式匹配坐标...");
|
|
||||||
Matcher matcher = COORD_PATTERN.matcher(polygonString);
|
Matcher matcher = COORD_PATTERN.matcher(polygonString);
|
||||||
int matchCount = 0;
|
int matchCount = 0;
|
||||||
|
|
||||||
@ -176,9 +155,7 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
double y = Double.parseDouble(matcher.group(2));
|
double y = Double.parseDouble(matcher.group(2));
|
||||||
pointsList.add(new PGpoint(x, y));
|
pointsList.add(new PGpoint(x, y));
|
||||||
matchCount++;
|
matchCount++;
|
||||||
System.out.println("正则匹配到点 " + matchCount + ": (" + x + ", " + y + ")");
|
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
System.err.println("坐标格式错误: " + matcher.group());
|
|
||||||
throw new IOException("坐标格式错误: " + matcher.group(), e);
|
throw new IOException("坐标格式错误: " + matcher.group(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -187,9 +164,7 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
|
|
||||||
// 方法2: 如果正则没有匹配到,尝试手动解析
|
// 方法2: 如果正则没有匹配到,尝试手动解析
|
||||||
if (pointsList.isEmpty()) {
|
if (pointsList.isEmpty()) {
|
||||||
System.out.println("正则匹配失败,尝试手动解析...");
|
|
||||||
pointsList = parsePointsManually(polygonString);
|
pointsList = parsePointsManually(polygonString);
|
||||||
System.out.println("手动解析到 " + pointsList.size() + " 个点");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pointsList.isEmpty()) {
|
if (pointsList.isEmpty()) {
|
||||||
@ -197,7 +172,6 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("字符串解析过程中发生异常: " + e.getMessage());
|
|
||||||
throw new IOException("字符串解析失败: " + e.getMessage(), e);
|
throw new IOException("字符串解析失败: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,12 +184,9 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
private List<PGpoint> parsePointsManually(String polygonString) throws IOException {
|
private List<PGpoint> parsePointsManually(String polygonString) throws IOException {
|
||||||
List<PGpoint> pointsList = new ArrayList<>();
|
List<PGpoint> pointsList = new ArrayList<>();
|
||||||
|
|
||||||
System.out.println("开始手动解析...");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 移除所有括号
|
// 移除所有括号
|
||||||
String cleaned = polygonString.replaceAll("[()]", "").trim();
|
String cleaned = polygonString.replaceAll("[()]", "").trim();
|
||||||
System.out.println("清理后字符串: " + cleaned);
|
|
||||||
|
|
||||||
if (cleaned.isEmpty()) {
|
if (cleaned.isEmpty()) {
|
||||||
throw new IOException("清理后字符串为空");
|
throw new IOException("清理后字符串为空");
|
||||||
@ -223,10 +194,9 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
|
|
||||||
// 分割点对
|
// 分割点对
|
||||||
String[] pointPairs = cleaned.split(",");
|
String[] pointPairs = cleaned.split(",");
|
||||||
System.out.println("分割出 " + pointPairs.length + " 个点对");
|
|
||||||
|
|
||||||
for (int i = 0; i < pointPairs.length; i++) {
|
for (String pair : pointPairs) {
|
||||||
String pointPair = pointPairs[i].trim();
|
String pointPair = pair.trim();
|
||||||
if (pointPair.isEmpty()) {
|
if (pointPair.isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -242,21 +212,16 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("点对 " + i + " 有效坐标数: " + validCoords.size());
|
|
||||||
|
|
||||||
if (validCoords.size() >= 2) {
|
if (validCoords.size() >= 2) {
|
||||||
try {
|
try {
|
||||||
double x = Double.parseDouble(validCoords.get(0));
|
double x = Double.parseDouble(validCoords.get(0));
|
||||||
double y = Double.parseDouble(validCoords.get(1));
|
double y = Double.parseDouble(validCoords.get(1));
|
||||||
PGpoint point = new PGpoint(x, y);
|
PGpoint point = new PGpoint(x, y);
|
||||||
pointsList.add(point);
|
pointsList.add(point);
|
||||||
System.out.println("手动解析点 " + i + ": (" + x + ", " + y + ")");
|
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
System.err.println("坐标解析失败 - 点对 " + i + ": " + pointPair);
|
|
||||||
throw new IOException("坐标解析失败: " + pointPair, e);
|
throw new IOException("坐标解析失败: " + pointPair, e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.err.println("坐标对格式不正确 - 点对 " + i + ": " + pointPair);
|
|
||||||
throw new IOException("坐标对格式不正确: " + pointPair);
|
throw new IOException("坐标对格式不正确: " + pointPair);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -266,7 +231,6 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("手动解析过程中发生异常: " + e.getMessage());
|
|
||||||
throw new IOException("手动解析失败: " + e.getMessage(), e);
|
throw new IOException("手动解析失败: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,17 +248,14 @@ public class PGpolygonSerializer extends JsonSerializer<PGpolygon> {
|
|||||||
List<PGpoint> filtered = new ArrayList<>();
|
List<PGpoint> filtered = new ArrayList<>();
|
||||||
int nullCount = 0;
|
int nullCount = 0;
|
||||||
|
|
||||||
for (int i = 0; i < points.length; i++) {
|
for (PGpoint point : points) {
|
||||||
if (points[i] != null) {
|
if (point != null) {
|
||||||
filtered.add(points[i]);
|
filtered.add(point);
|
||||||
} else {
|
} else {
|
||||||
nullCount++;
|
nullCount++;
|
||||||
System.err.println("发现第 " + i + " 个点为 null");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("过滤结果: " + filtered.size() + " 个有效点, " + nullCount + " 个 null 点");
|
|
||||||
|
|
||||||
if (filtered.isEmpty()) {
|
if (filtered.isEmpty()) {
|
||||||
throw new IOException("过滤后没有剩余有效点");
|
throw new IOException("过滤后没有剩余有效点");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user