jacksonjava轉jsonhibernate造成的堆疊溢位無限遞迴問題

bitree1發表於2017-03-20
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/bitree1/article/details/64128051

controller 返回 geometry 欄位報錯

Infinite recursion (StackOverflowError) 

(through reference chain: com.vividsolutions.jts.geom.Polygon[“envelope”]->

com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->
com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->
com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->
->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]-
>com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygo
n[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon
[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon
[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope
“]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsol
utions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->
com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->
com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->
com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->
com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->
com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->com.vividsolutions.jts.geom.Polygon[“envelope”]->
三月 19, 2017 2:27:06 下午 org.apache.catalina.core.ApplicationDispatcher invoke
@JsonIgnore
@JSONField(serialize=false,deserialize=false)
@Type(type = “org.hibernate.spatial.GeometryType”)
@Column(name = “Shape”,columnDefinition=”Geometry”)
private Polygon Shape;

/**
* Shape()
* wkt格式
*/
@Transient
private String wktShape;

@JsonIgnore
public Polygon getShape() {
return Shape;
}
@JsonIgnore
public void setShape(Polygon shape) {
Shape = shape;
}
public String getWktShape() {
return Shape.toText();
}
public void setWktShape(String wktShape) {
this.wktShape = wktShape;
}
  注 :(1)get set 方法上面的 @JsonIgnore用於返回大寫字母的欄位,防止被轉小寫,可與欄位上面的@JsonProperty組合

WARN  o.s.w.s.h.SimpleMappingExceptionResolver – Handler execution resulted in exception: Could not write content:

嚴重: Servlet.service() for servlet jsp threw exception

原因:jackson java轉json hibernate  造成的堆疊溢位 無限遞迴問題

解決方法

思路:遮蔽geometry 欄位輸出,重新定義一個欄位獲取 上一個geometry欄位的wkt格式字串

程式碼:如我需要輸出shape欄位為geometry格式 

    (1)則在弄一個wktShape欄位作為代替輸出

    (2)欄位上面的@JsonIgnorey用於前端返回json的時候遮蔽輸出欄位




相關文章