【问题标题】:Spring Data rest doesn't call a getter with a custom propertySpring Data rest 不会调用具有自定义属性的 getter
【发布时间】:2017-04-27 15:02:45
【问题描述】:

有这样的实体:

@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?

【问题讨论】:

    标签: java spring spring-data spring-data-rest serialization


    【解决方案1】:

    如果关联类型未链接到存储库,则关联在 spring data-rest 中显示为 hal-links。

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

    实体 id 也会被跳过(默认情况下),因为您可以在相应的 self 中找到 id 链接。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 2018-04-18
      • 2018-03-17
      相关资源
      最近更新 更多