MOXy有杰克逊@JsonIgnore标签的类似物吗?



在JAX-RS(泽西(应用程序中,我有一个Java类,其实例既可以存储在SQL数据库中(使用OpenJPA(,也可以作为JSON文本通过网络发送(在RESTful应用程序中(。这个类有一个字段,它有一个setter和getter(用于OpenJPA(,但我不想将其转换为JSON。我知道,如果我在泽西岛使用 Jackson 进行 Java 对象到 JSON 的转换,我可以使用 @JsonIgnore 标签来实现这个目的,即。

// This is the main class sent in REST responses; inside a Parent description, a number of 
// Child instances are described
@XmlRootElement(name = "parent") 
@Entity  
public class Parent  {
...
@OneToMany(
mappedBy="parent", cascade=CascadeType.ALL, orphanRemoval=true, fetch=FetchType.EAGER)
private Vector<Child> children = new  Vector<>();
...
}
@Entity
public class Child implements Serializable {
...
// Don't want to serialize Parent, to avoid an infinite loop!
@JsonIgnore
@ManyToOne(fetch = FetchType.EAGER)
private Parent parent;
public Parent getParent() { return parent; }
public void setParent(Parent _parent) { parent = _parent; }
...
}

但是,我的泽西岛设置使用"默认"JSON转换工具,该工具是MOXy而不是Jackson(根据 https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/index.html,第9.1节(。因此,此设置没有@JsonIgnore标记。Jersey + MOXy框架中是否有类似的标签?我以为@XmlTransient可能会解决问题,但事实并非如此。

作为一个实际的解决方案,我简单地将getParent()重命名为xgetParent();显然OpenJPA不受影响,但MOXy不再尝试转换这个字段。但这当然是一个荒谬的笨拙解决方案,最终肯定会破坏一些东西。

@XmlTransient工作,但使用getter。我用EclipseLink/MOXy 2.7.7测试了它。

所以试试

@XmlTransient
public Parent getParent() { return parent; }

最新更新