Java 从Spring数据JpaRepository返回@id字段

Java 从Spring数据JpaRepository返回@id字段,java,json,spring,jpa,Java,Json,Spring,Jpa,我继承了一个巨大的Java应用程序,在尝试找到解决方法时遇到了不少问题。关于Spring JpaRepository,我有一个具体的问题。请注意,我刚从春天开始,还不是很确定 我有一个带有@RepositoryRestResource注释的存储库 @RepositoryRestResource public interface GoodsReceiptRepository extends JpaRepository<GoodsReceipt, GoodsReceiptId> { 及

我继承了一个巨大的Java应用程序,在尝试找到解决方法时遇到了不少问题。关于Spring JpaRepository,我有一个具体的问题。请注意,我刚从春天开始,还不是很确定

我有一个带有@RepositoryRestResource注释的存储库

@RepositoryRestResource
public interface GoodsReceiptRepository extends JpaRepository<GoodsReceipt, GoodsReceiptId> {

JSON响应中不会返回带有@Id注释的任何字段:

@Id @Column(name = "LGNTCC")
private String accountingArea;
如何获取这些ID字段

如果我删除@Id,我会得到我想要的,但我不敢这样做,因为我无法判断这会对应用程序产生什么影响


干杯

您似乎正在使用
Spring数据Rest
您看到的是它的默认行为

您可以通过执行以下操作自定义此行为:

import org.springframework.context.annotation.Configuration;
导入org.springframework.data.rest.core.config.RepositoryRestConfiguration;
导入org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
@配置
公共类RepositoryConfig扩展了RepositoryRestConfigurerAdapter{
@凌驾
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration配置){
exposeIdsFor(yourclassnamegosher.class);
}
}

尝试为每个实体创建一个DTO,然后
avisopositiondto.setId(avisoPosition.getId())
只返回每个实体的DTO

是的,绝对不要删除
@Id
注释。。。JPA需要知道什么定义了一个唯一的记录,试着将
@JsonProperty(“id”)
添加到这些字段中……老实说,这些字段可能已经转换为DTO了。。。Spring默认情况下可能会忽略正在序列化的@id字段?我很想这样做,但我在不同的文件中有大约200个@Entity条目,在大约10个项目中,我不知道它们是如何协同工作的!我会在将来考虑这样做-谢谢。马可斯-谢谢你,这是为我做的。我需要一点时间来找到正确的配置文件(大约有10个文件扩展了RepositoryRestConfigurerAdapter)——不过现在可以工作了。非常感谢。
@Entity
@Table(name = GoodsReceipt.TABLE)
@IdClass(GoodsReceiptId.class)
public class GoodsReceipt implements Serializable { 
@Id @Column(name = "LGNTCC")
private String accountingArea;