Spring Data Rest许多对多个树投影映射

时间:2017-08-15 01:36:53

标签: java spring spring-data-rest

我有一个名为ContentPath的实体,可能有相同类型的父类,以及相同类型的儿子,用以下内容表示:

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "NAME", length = 50)
    @NotNull
    private String name;

    @Column(name = "DESCRIPTION")
    private String description;

    @ManyToOne
    @JoinColumn(name="CONTENT_PATH_ID")
    public ContentPath contentPath;

    @OneToMany(mappedBy="contentPath")
    public Set<ContentPath> contentPaths;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "ACTIVITY_CONTENT_PATH",
            joinColumns = {@JoinColumn(name = "CONTENT_PATH_ID", referencedColumnName = "ID")},
            inverseJoinColumns = {@JoinColumn(name = "ACTIVITY_ID", referencedColumnName = "ID")})
    private Set<Activity> activities;

我有我的ContentPathRepository,它将其公开为API。

@RepositoryRestResource(collectionResourceRel = "contentPaths", path = "contentPaths", excerptProjection = ContentPathProjection.class)
public interface ContentPathRestRepository extends PagingAndSortingRepository<ContentPath, Long> {

}

和我的投影,它应该正确格式化我的对象。

@Projection(name = "contentPathProjection", types = ContentPath.class)
public interface ContentPathProjection {
    Long getId();
    String getName();
    Set<ContentPath> getContentPaths();
}

我原本希望得到一个内容包含ContentPath的ContentPaths列表,并且我获得了成功,但它没有带ID,它只带来了Name和Description,这很奇怪,因为我的投影没有有描述。

当前回复:

"name": "BOOK 1",
"id": 1,
"contentPaths": [
    {
        "name": "UNIT 1",
        "description": "UNIT 1 description"
    },
    {
        "name": "UNIT 2",
        "description": "UNIT 2 description"
    }
]

为什么会这样?如何解决?

1 个答案:

答案 0 :(得分:1)

这是SDR的正常行为。默认情况下,它不会显示id。要启用它,只需注册这样的bean:

@Bean
public RepositoryRestConfigurerAdapter repositoryRestConfigurerAdapter() {
    return new RepositoryRestConfigurerAdapter() {
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
            config.exposeIdsFor(ContentPath.class);
            super.configureRepositoryRestConfiguration(config);
        }
    };
}

关于description - 你有这个字段:

@Column(name = "DESCRIPTION")
private String description;