我正在使用带hibernate 4.2.6和spring 4.0.1的java开发应用程序。我的应用程序是@Entity
public class Calk {
@Id
@GeneratedValue
private Long id;
@OneToMany(mappedBy="calk", fetch=FetchType.LAZY)
List<BaseLayer> baseLayer = new ArrayList<BaseLayer>();
public void addBaseLayer(BaseLayer baseLayer){
this.baseLayer.add(baseLayer);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JsonIgnore
public List<BaseLayer> getBaseLayer() {
return baseLayer;
}
public void setBaseLayer(List<BaseLayer> baseLayer) {
this.baseLayer = baseLayer;
}
}
应用程序。为此,我使用杰克逊。我的实体如下:
Calk.java:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="layer")
@JsonSubTypes(
{
@JsonSubTypes.Type(value=PointLayer.class, name="point")
})
@DiscriminatorValue("BaseLayerDiscriminator")
public class BaseLayer {
@Id
@GeneratedValue
protected Long gid;
@ManyToOne(fetch = FetchType.LAZY)
protected Calk calk;
public Long getGid(){
return gid;
}
public void setGid(Long gid){
this.gid = gid;
}
@JsonIgnore
public Calk getCalk(){
return calk;
}
public void setCalk(Calk calk){
this.calk = calk;
}
}
BaseLayer.java:
BaseLayer.java
现在我有一个从@Entity
@DiscriminatorValue("PointDiscriminator")
public class PointLayer extends BaseLayer{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
延伸的课程如下:
PointLayer.java:
{"layer": "point", "calk":{"id":1}, "name": "itsme"}
现在我按照以下方式创建一个json,然后将其发送到控制器:
@RequestMapping("\test")
public String test(@RequestBody BaseLayer baseLayer){
System.out.println(baseLayer.getName());// this print "itsme"
Calk calk = baseLayer.getCalk();//it return null
if(calk == null)
return "its null";
else
return "its not null";
}
现在控制器定义如下:
its not null
当我们调用控制器时,它返回calk
。 null
不应为@JsonIgnore
。
问题出在哪里?
更新:
当我在getCalk
删除getCalk
时,它运行正常。但为什么?我想忽略setCalk
但不要忽略array<vector<array<double, L> >, N>
。
答案 0 :(得分:0)
@JsonIgnore
如下:
@JsonIgnore
public List<BaseLayer> getBaseLayer() {
return baseLayer;
}
默认情况下将Ignore设置为以下:
public void setBaseLayer(List<BaseLayer> baseLayer) {
this.baseLayer = baseLayer;
}
您必须在设置功能之上添加@JsonProperty("baseLayer")
,如下所示:
@JsonProperty("baseLayer")
public void setBaseLayer(List<BaseLayer> baseLayer) {
this.baseLayer = baseLayer;
}