[英]Spring Boot JPA with Hibernate Spatial self-reference leading to cycle
我目前正在编写一项服务,我可以在其中存储带有一些数据的地理空间点。 我有一个“数据点”class,看起来像这样:
@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 Boot 通过 API 路径简单地将带有一些信息的点添加到 PostGIS 数据库。 我构建了一个 controller,如下所示:
@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
,它只是充当 controller 之间的中间人,其中saveDataPoint()
如下所示:
public void saveDataPoint(DataPoint myPoint) {
dataPointRepository.save(myPoint);
}
和DataPointRepository
,它看起来像这样:
@Repository
public interface DataPointRepository extends JpaRepository<DataPoint, Integer> {
}
但是,当我访问我的添加链接时,出现此错误:
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
及其双重使用,但是,它已用于实体来回链接的情况,我在这里看不到这种情况,事实上,错误似乎甚至没有循环,所以这里发生了什么?
我遇到了同样的问题。 它是循环的,因为 Point 有一个字段 firstPoint 再次引用 Point。 我能够通过安装此 postgis-geojson 来解决问题: https://jitpack.io/p/stephenbrough/postgis-geojson
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.