2017-03-02 102 views
1

在我的Spring Data Rest應用程序中,當定義了excerptProjection時,@JsonBackReference被忽略。使用摘錄投影時忽略@JsonBackreferenceProject

當調用GET /foos/{id}我得到的迴應:(_link元素都在這裏省略)

{ 
    "text": "Foo", 
    "additionalText": "Additional Text", 
    "bar": { 
    "text": "Bar", 
    "_embedded": { 
     "foo": { 
     "text": "Foo", 
     "bar": { 
      "text": "Bar" 
     } 
     } 
    } 
    } 
} 

考慮:

@Entity 
public class Foo { 

    @Id 
    @GeneratedValue 
    private Long id; 

    private String text; 

    private String additionalText; 

    @JsonManagedReference 
    @OneToOne(mappedBy = "foo", cascade = ALL) 
    private Bar bar; 
} 

@Entity 
public class Bar { 

    @Id 
    @GeneratedValue 
    private Long id; 

    private String text; 

    @JsonBackReference 
    @OneToOne 
    private Foo foo; 
} 

與存儲庫:

@RepositoryRestResource(excerptProjection = FooPublicProjection.class) 
public interface FooRepository extends CrudRepository<Foo, Long> {} 

如果我刪除excerptProjection定義,我得到以下結果:

{ 
    "text": "Foo", 
    "additionalText": "Additional Text", 
    "bar": { 
    "text": "Bar" 
    } 
} 

我能做些什麼來讓Spring數據休息不會使自己的子節點內的Foo

回答

0

這是一個解決方案,提供您使用傑克遜(我猜你會這樣做)。 在我的項目中,我用JSOG來處理我所有的循環關係。我認爲它也可以解決您的問題。

@Entity 
@JsonIdentityInfo(generator = JSOGGenerator.class) 
public class Foo { 

    @Id 
    @GeneratedValue 
    private Long id; 

    private String text; 

    private String additionalText; 

    @JsonManagedReference 
    @OneToOne(mappedBy = "foo", cascade = ALL) 
    private Bar bar; 
} 

和:

@Entity 
@JsonIdentityInfo(generator = JSOGGenerator.class) 
public class Bar { 

    @Id 
    @GeneratedValue 
    private Long id; 

    private String text; 

    @JsonBackReference 
    @OneToOne 
    private Foo foo; 
} 

請注意,如果你的客戶是JavaScript中,有一個補充JSOG LIB,可以解析您的JSON和自動解決週期。