在我的spring-data-rest应用程序中,我有以下实体
@Entity
public class com.foo.client.Foo {
@Id
@Column(name = "id", nullable = false, length = 48)
public String id = UUID.randomUUID().toString();
@OneToOne
public Bar bar;
}
@Entity
public class Bar {
@Id
@Column(name = "id", nullable = false, length = 48)
public String id = UUID.randomUUID().toString();
public String name;
}对于每个实体类,我都有JpaRepository:
@RepositoryRestResource(collectionResourceRel = "foos", path = "foos")
public interface FooRepository extends JpaRepository<Foo, String> {
}
@RepositoryRestResource(collectionResourceRel = "bars", path = "bars")
public interface BarRepository extends JpaRepository<Bar, String> {
}我使用以下命令为Foo和Bar创建了一个实例:
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:foo:test:0"}' http://localhost:8000/foos
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:bar:test:0", "name" : "0"}' http://localhost:8000/bars然后,我使用以下命令将Bar实例与Foo实例关联:
curl -i -X PUT -d "http://localhost:8000/bars/urn:bar:test:0\n"
-H "Content-Type:text/uri-list" "http://localhost:8000/foos/urn:foo:test:0/bar"当在相同的spring-data-rest服务端点中定义这两个实体时,一切正常,我可以使用以下命令获取与foo实例关联的bar实例:
curl -i -X GET -H "Content-Type:application/json" "http://localhost:8000/foos/urn:foo:test:0/bar"问:查看存储实体的postgresdb,我看到一个关联表FOO_BAR,其中有两列保存每个实体的id。但是,我没有看到bar的URL存储在哪里,我想知道它存储在哪里。
现在,如果我将我的应用程序拆分成两个单独的spring-data-rest服务,一个是Foo的foo-service,另一个是bar的bar-service,分别位于不同的端口,并且还在两个项目之间拆分Repository类,那么创建关联将不起作用,我会得到一个404。修改后的代码如下:
我使用以下命令为Foo和Bar创建了一个实例:
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:foo:test:0"}' http://localhost:8000/foos
curl -i -X PUT -H "Content-Type:application/json"
-d '{"id": "urn:bar:test:0", "name" : "0"}' http://localhost:8001/bars然后,我使用以下命令将Bar实例与Foo实例关联:
curl -i -X PUT -d "http://localhost:8001/bars/urn:bar:test:0\n"
-H "Content-Type:text/uri-list" "http://localhost:8000/foos/urn:foo:test:0/bar"当Foo和Bar由不同的spring-data-rest服务管理时,上面的最后一个请求给了我一个404。
我怎样才能让第二个案例工作呢?
请注意,我在示例中使用了this优秀资源。
发布于 2018-08-01 08:03:03
事实证明,当代码被拆分到两个独立的服务(同一多项目gradle中的独立模块)时,我还需要拥有所有者实体(foo- BarRepository )的服务才能拥有拥有实体(Bar)的服务。当我将BarRepository从bar-service模块复制到foo-service模块时,我不再获得404,并且可以在API中观察到Foo与Bar之间的关系。当然,这只有在foo-service和bar-service共享相同的数据库时才有效(目前在我的例子中是这样的)。
我想要一个描述如何在两个服务不共享数据库的一般情况下解决这个问题的答案。
https://stackoverflow.com/questions/51602270
复制相似问题