Hibernate @JsonProperty到多个输出字段

时间:2013-01-17 15:01:31

标签: json spring hibernate serialization

我在Hibernate注释中有一个问题。我有一个实体,因为我有一个列,我使用@JsonSerialize将值转换为不同的格式并使用@JsonProperty我将它写入输出json中的不同字段:

@Transient
@Type(type = "org.hibernatespatial.GeometryUserType")
@Column(name = "the_geom", columnDefinition = "Geometry")
Geometry gml;

@JsonProperty("wkt")
@JsonSerialize(using = JsonGeometrySerializer.class)
public Geometry getGeom() {
    return geom;
}

public void setGeom(Geometry geom) {
    this.geom = geom;
}

现在我想为来自the_geom列的相同几何创建一个不同的属性,如@JsonProperty(“gml”),并将其写入输出json中的不同字段。有没有办法可以在@JsonProperty中指定多个值?我尝试使用getter / setter创建另一个变量并使用@Trancient但不确定如何将the_geom值发送到该序列化器。请帮忙。

我尝试过这样的事情并没有奏效:

    @Transient
@Type(type = "org.hibernatespatial.GeometryUserType")
@Column(name = "the_geom", columnDefinition = "Geometry")
Geometry gml;

@JsonSerialize(using = JsonGeometrySerializer.class)
public Geometry getGml() {
    return gml;
}

public void setGml(Geometry gml) {
    this.gml = gml;
}

在第一种情况下,几何应该转换为wkt并添加到json中,在第二种情况下,几何应该转换为GML格式并添加到gml中。我可以同时使用相同的JsonSerializer类,还是必须编写一个新类?

1 个答案:

答案 0 :(得分:2)

由于你正在将@JsonSerializer附加到方法,是什么阻止你创建另一个getXXX方法返回你想要的任何东西,包括相同的值?

相关问题