我已经构建了两个spring-boot应用程序,带有休息资源的服务器端spring-boot微服务和使用Feign Clients消耗HATEOAS feed的客户端spring-boot微服务应用程序。
我两边都有两个实体对象Aggregate和Gateway。网关在Aggregate对象
中只要我没有为Gateway对象提供@RepositoryRestResource接口,我就可以通过Aggregate检索Gateway对象,但如果我有注释,我就无法在客户端的Aggregate对象上列出Gateway。我注意到这是因为服务器端HATEOAS feed为Gateway上的Gateway添加了链接而不是Gateway的Json结构。
无论如何,我仍然可以从Aggregate对象获取Gateway对象,同时拥有Gateway对象的@RepositoryRestResource接口?或者有没有办法配置Feign客户端从链接填充网关对象?
实施例.. 来自客户http://localhost:9999/aggregates/
使用GatewayRepository上的@RepositoryRestResource注释
[
{
"id": "a65b4bf7-6ba5-4086-8ca2-783b04322161",
"gateway": null, //<-- Gateway is null here
.......
没有GatewayRepository上的@RepositoryRestResource注释
[
{
"id": "a65b4bf7-6ba5-4086-8ca2-783b04322161",
"gateway": { //<-- Gateway id and properties are there now on Aggregate object
"id": "4a857a7a-2815-454c-a271-65bf56dc6f79",
.......
来自服务器http://localhost:8000/aggregates/
使用GatewayRepository上的@RepositoryRestResource注释
{
"_embedded": {
"aggregates": [
{
"id": "a65b4bf7-6ba5-4086-8ca2-783b04322161",
"_links": {
"self": {
"href": "http://localhost:8000/aggregates/a65b4bf7-6ba5-4086-8ca2-783b04322161"
},
"gateway": { //<-- Gateway becomes a link here
"href": "http://localhost:8000/aggregates/a65b4bf7-6ba5-4086-8ca2-783b04322161/gateway"
},
.......
没有GatewayRepository上的@RepositoryRestResource注释
"_embedded": {
"aggregates": [
{
"id": "b5171138-4313-437a-86f5-f70b2b5fcd22",
"gateway": { //<-- Gateway id and properties are there now on Aggregate object
"id": "3608726b-b1b1-4bd4-b861-ee2bf5c0cc03",
.......
以下是模型对象的服务器端实现
@Entity
class Aggregate extends TemplateObject {
@OneToOne(cascade = CascadeType.MERGE)
private Gateway gateway;
.......
}
@Entity
class Gateway extends TemplateObject {
@NotNull
@Column(unique = true)
private String name;
.......
}
服务器端休息存储库
@RepositoryRestResource
interface GatewayRepository extends JpaRepository<Gateway, String> {
Optional<Gateway> findByName(@Param("name") String name);
}
@RepositoryRestResource
interface AggregateRepository extends JpaRepository<Aggregate, String> {
Optional<Aggregate> findByName(@Param("name") String name);
}
(在端口8000上使用这些Rest资源)
在客户端,我在模型dto对象上具有相同的植入
class Gateway extends TemplateObject {
@NotNull
private String name;
.......
}
class Aggregate extends TemplateObject {
private Gateway gateway;
.......
}
并假装客户
@FeignClient("billing-service/gateways")
interface GatewayService extends GenericService<Gateway> {
}
@FeignClient("billing-service/aggregates")
interface AggregateService extends GenericService<Aggregate> {
}
(在端口9999客户端控制器上使用这些Feign客户端)
在此先感谢您的帮助,我们非常感谢您的建议和建议