Spring 数据 rest 不会调用带有自定义 属性 的 getter

Spring Data rest doesn't call a getter with a custom property

拥有如下实体:

@Entity
public class Player {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;
    private String userName;

    @OneToMany(mappedBy="player", fetch=FetchType.EAGER)
    private Set<Participation> participations;

    public long getId() {
        return id;
    }

    public String getUserName() {
        return userName;
    }

    public Set<Participation> getParticipations() {
        return participations;
    }
}

当我使用调试器访问 url http://localhost:8080/rest/players/1 时,我可以看到 getUserName 是如何被调用的(我猜是通过序列化程序调用的)。未调用 getIdgetParticipations

这种行为对我来说没问题,我的意思是,这只是好奇,为什么不调用这些 getter?

如果关联类型未linked 到存储库,则关联在spring data-rest 中公开为 hal-links。

它们在 HAL“_links”资源下可用。 (参考spring data-rest projections & excerpts)。

实体 ID 也被跳过(默认情况下),因为您可以在相应的 self 中找到该 ID link.

要公开 ID,您只需配置 RepositoryRestConfigurerAdapter 以使用以下方式公开 ID:

@Configuration
public class DataRestConfiguration extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(Entity.class);
    }
}