Spring boot Spring data rest和jpa@OneToMany duplicates"_链接“;
我对SpringDataREST与SpringDataJPA一起使用有一个问题。我使用的是Spring boot 1.4.4.RELEASE 以下是我的spring数据rest存储库:Spring boot Spring data rest和jpa@OneToMany duplicates"_链接“;,spring-boot,spring-data-jpa,spring-data-rest,Spring Boot,Spring Data Jpa,Spring Data Rest,我对SpringDataREST与SpringDataJPA一起使用有一个问题。我使用的是Spring boot 1.4.4.RELEASE 以下是我的spring数据rest存储库: public interface ProfileJpaRepository extends JpaRepository<Profile, Long> { } 下面是我点击的结果: 我认为这是一个问题,因为每个属性下都指定了“\u链接”。相反,我本以为会发生这样的事情: { "_embedded"
public interface ProfileJpaRepository extends JpaRepository<Profile, Long> {
}
下面是我点击的结果:
我认为这是一个问题,因为每个属性下都指定了“\u链接”
。相反,我本以为会发生这样的事情:
{
"_embedded" : {
"profiles" : [ {
"description" : "description-value",
"attributes" : [ {
"uri" : "uri-a",
"datas" : [ "uri-a-value" ]
}, {
"uri" : "uri-b",
"datas" : [ "uri-b-value" ]
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8880/profiles/1"
},
"profile" : {
"href" : "http://localhost:8880/profiles/1"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8880/profiles"
},
"profile" : {
"href" : "http://localhost:8880/profile/profiles"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
请注意,我一直在从MongoRepository切换到JpaRepository,并且使用MongoRepository,这些“\u链接”
没有“复制”
有人能解释一下吗?我是否对我的JPA实体有误解?我是否需要在rest存储库中配置某些内容
如果您需要,可以在此处找到有关依赖项版本的更多信息,我没有覆盖这些()
谢谢 为了解决这个问题,我一直在使用投影(更多信息如下:)。我在发帖时没有意识到这一点 我必须注释我的存储库,并告诉它使用我的InlineAttributes投影:
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(excerptProjection = InlineAttributes.class)
public interface ProfileJpaRepository extends JpaRepository<Profile, Long> {
}
还有我必须创建的InlineAttributes投影类。我指定了顺序,因为它与以前不同:
import org.springframework.data.rest.core.config.Projection;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@Projection(name = "inlineAttributes", types = { Profile.class })
@JsonPropertyOrder({ "description", "attributes" })
public interface InlineAttributes {
public String getDescription();
public Set<Attribute> getAttributes();
}
您知道在您的代码中如何/为什么生成_链接吗?我假设在JSON序列化过程中会发生这种情况,但我从未遇到过使用默认配置的情况。@TorstenN.,这是由ProfileJpaRepository自动完成的,Spring Data Rest会自动扩展JpaRepository,并“使用HAL作为媒体类型为您的域模型公开一个可发现的Rest API”。
{
"_embedded" : {
"profiles" : [ {
"description" : "description-value",
"attributes" : [ {
"uri" : "uri-a",
"datas" : [ "uri-a-value" ],
"_links" : {
"profile" : {
"href" : "http://localhost:8880/profiles/1"
}
}
}, {
"uri" : "uri-b",
"datas" : [ "uri-b-value" ],
"_links" : {
"profile" : {
"href" : "http://localhost:8880/profiles/1"
}
}
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8880/profiles/1"
},
"profile" : {
"href" : "http://localhost:8880/profiles/1"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8880/profiles"
},
"profile" : {
"href" : "http://localhost:8880/profile/profiles"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
{
"_embedded" : {
"profiles" : [ {
"description" : "description-value",
"attributes" : [ {
"uri" : "uri-a",
"datas" : [ "uri-a-value" ]
}, {
"uri" : "uri-b",
"datas" : [ "uri-b-value" ]
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8880/profiles/1"
},
"profile" : {
"href" : "http://localhost:8880/profiles/1"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8880/profiles"
},
"profile" : {
"href" : "http://localhost:8880/profile/profiles"
}
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(excerptProjection = InlineAttributes.class)
public interface ProfileJpaRepository extends JpaRepository<Profile, Long> {
}
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name = "ATTRIBUTE")
public class Attribute {
...
@ManyToOne(fetch = FetchType.EAGER)
@JsonIgnore
private Profile profile;
...
}
import org.springframework.data.rest.core.config.Projection;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@Projection(name = "inlineAttributes", types = { Profile.class })
@JsonPropertyOrder({ "description", "attributes" })
public interface InlineAttributes {
public String getDescription();
public Set<Attribute> getAttributes();
}
http://localhost:8880/profiles?projection=inlineAttributes