我目前正在编写一个服务,在那里我可以用一些数据存储一个地理空间点。我有一个"dataPoint“类,如下所示:
@Entity
@Table(name = "datapoint")
public class DataPoint {
@Id
int dataPoint_id;
@Column(name = "body")
String body;
@Column(name = "location", columnDefinition = "Geometry")
PGgeometry location;
@Column(name = "deleted")
boolean deleted;
//Getters and Setters...我试图使用Spring通过API路径向PostGIS数据库添加一个带有一些信息的点。我已经建立了一个控制器,它看起来像这样:
@RestController
@RequestMapping(value = "/dataPoint")
public class DataPointController {
@Autowired
private DataPointService myPointService;
@RequestMapping(value = "/add/{body}/{latitude}/{longitude}/")
public DataPoint addDataPoint(@PathVariable String body, @PathVariable double latitude, @PathVariable double longitude){
DataPoint myPoint = new DataPoint();
myPoint.setBody(body);
PGgeometry geometry = new PGgeometry();
try {
geometry.setValue("POINT("+longitude +" " + latitude+")");
geometry.setType("POINT");
// Debugging Stuff
System.out.println("GEOMETRY VALUE LOOK: {{{{ " + geometry.getValue() + " " + geometry.getType());
} catch (SQLException e) {
e.printStackTrace();
}
myPoint.setLocation(geometry);
myPointService.saveDataPoint(myPoint);
return myPoint;
}它反过来链接到一个DataPointService,它只是充当控制器之间的中间角色,saveDataPoint()看起来是这样的:
public void saveDataPoint(DataPoint myPoint) {
dataPointRepository.save(myPoint);
}以及DataPointRepository,它看起来是这样的:
@Repository
public interface DataPointRepository extends JpaRepository<DataPoint, Integer> {
}但是,当我访问add链接时,会得到以下错误:
Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Direct self-reference leading to cycle (through reference chain: com.testing.model.DataPoint["location"]->org.postgis.PGgeometry["geometry"]->org.postgis.Point["firstPoint"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: com.testing.model.DataPoint["location"]->org.postgis.PGgeometry["geometry"]->org.postgis.Point["firstPoint"])我在一些例子中看到了@JsonBackReference及其对偶的用法,然而,在实体被来回链接的情况下,它被使用了,我在这里没有看到这种情况,事实上,错误似乎并不是循环的,那么这里发生了什么呢?
发布于 2022-02-10 19:47:30
我遇到了同样的问题。它是循环的,因为Point有一个字段firstPoint,它再次引用Point。我能够通过安装postgis-geojson:https://jitpack.io/p/stephenbrough/postgis-geojson来解决这个问题。
https://stackoverflow.com/questions/37709340
复制相似问题