Spring 数据剩余 - 关联资源自身 link
Spring Data Rest - Self link of association resource
这是 GET 请求的 JSON 响应:
{
...
"_links": {
"self": {
"href": "http://localhost:8080/persons/1"
},
"person": {
"href": "http://localhost:8080/persons/1{?projection}",
"templated": true
},
"anotherResource": {
"href": "http://localhost:8080/persons/1/anotherResource"
}
}
}
问题是,我需要 "anotherResource" 的自我 link。而不是:
"href": "http://localhost:8080/persons/1/anotherResource"
我需要 link 类似的东西:
"href": "http://localhost:8080/anotherResources/2"
我知道我可以通过提出额外的要求来实施它。但是这个解决方案在我的情况下不是practical/possible,我需要有很多数据并且对每个项目执行额外的请求并不好。
任何 suggestions/solutions?
端点评论
您没有使用@RequestMapping 注解来声明 REST 端点吗?
我会尝试使用不同的@RequestMapping 调用来定义不同的端点。
通过这种方式,程序可以有一个 /persons
的 RequestMapping,其中任何 persons-related 操作都关联到其各自的端点,并定义另一个 "root"(强制引号,但你确保到达我要去的地方)为 /anotherresources
映射,您可以在其中添加必要的端点。
响应处理
如果您只需要更改用户在访问 GET 端点时收到的 JSON,您只需预处理参数并更新 JSON,然后再将其实际发送回用户.
但是,当然,如果您重写 JSOn 而没有使用现有端点支持 JSON 中参数的新值,则用户在尝试访问该 URI 时会遇到麻烦.如果您只需要形成 JSON,我会假设 /anotherresources
的端点已经存在。
有关@RequestMapping 的详细信息,请务必visit the Spring Docs website。
希望对你有所帮助!
您可以尝试使用ResourceProcessor with RepositoryEntityLinks构建您需要的link:
@Component
public class PersonResourceProcessor implements ResourceProcessor<Resource<Person>> {
private RepositoryEntityLinks entityLinks;
public PersonResourceProcessor(RepositoryEntityLinks entityLinks) {
this.entityLinks = entityLinks;
}
@Override
public Resource<Person> process(Resource<Person> resource) {
Person person = resource.getContent();
AnotherResource anotherResource = person.getAnotherResource()
Link link = entityLinks.linkForSingleResource(anotherResource).withRel("anotherResource");
resource.add(link);
return resource;
}
}
但这里要小心,因为如果资源 person
没有急切地嵌套 anotherResource
你可以捕获 LazyInitializationException
(不确定,但请检查它... ) 或为每个 person.getAnotherResource()
调用 (the N+1 queries issue) 获取对数据库的额外查询。这就是为什么最好使用像“/persons/1/anotherResource”这样的相对 link。
这是 GET 请求的 JSON 响应:
{
...
"_links": {
"self": {
"href": "http://localhost:8080/persons/1"
},
"person": {
"href": "http://localhost:8080/persons/1{?projection}",
"templated": true
},
"anotherResource": {
"href": "http://localhost:8080/persons/1/anotherResource"
}
}
}
问题是,我需要 "anotherResource" 的自我 link。而不是:
"href": "http://localhost:8080/persons/1/anotherResource"
我需要 link 类似的东西:
"href": "http://localhost:8080/anotherResources/2"
我知道我可以通过提出额外的要求来实施它。但是这个解决方案在我的情况下不是practical/possible,我需要有很多数据并且对每个项目执行额外的请求并不好。
任何 suggestions/solutions?
端点评论
您没有使用@RequestMapping 注解来声明 REST 端点吗?
我会尝试使用不同的@RequestMapping 调用来定义不同的端点。
通过这种方式,程序可以有一个 /persons
的 RequestMapping,其中任何 persons-related 操作都关联到其各自的端点,并定义另一个 "root"(强制引号,但你确保到达我要去的地方)为 /anotherresources
映射,您可以在其中添加必要的端点。
响应处理
如果您只需要更改用户在访问 GET 端点时收到的 JSON,您只需预处理参数并更新 JSON,然后再将其实际发送回用户.
但是,当然,如果您重写 JSOn 而没有使用现有端点支持 JSON 中参数的新值,则用户在尝试访问该 URI 时会遇到麻烦.如果您只需要形成 JSON,我会假设 /anotherresources
的端点已经存在。
有关@RequestMapping 的详细信息,请务必visit the Spring Docs website。
希望对你有所帮助!
您可以尝试使用ResourceProcessor with RepositoryEntityLinks构建您需要的link:
@Component
public class PersonResourceProcessor implements ResourceProcessor<Resource<Person>> {
private RepositoryEntityLinks entityLinks;
public PersonResourceProcessor(RepositoryEntityLinks entityLinks) {
this.entityLinks = entityLinks;
}
@Override
public Resource<Person> process(Resource<Person> resource) {
Person person = resource.getContent();
AnotherResource anotherResource = person.getAnotherResource()
Link link = entityLinks.linkForSingleResource(anotherResource).withRel("anotherResource");
resource.add(link);
return resource;
}
}
但这里要小心,因为如果资源 person
没有急切地嵌套 anotherResource
你可以捕获 LazyInitializationException
(不确定,但请检查它... ) 或为每个 person.getAnotherResource()
调用 (the N+1 queries issue) 获取对数据库的额外查询。这就是为什么最好使用像“/persons/1/anotherResource”这样的相对 link。