简体   繁体   English

Spring Data Rest-关联资源的自链接

[英]Spring Data Rest - Self link of association resource

This is the JSON-response of a GET-Reqeust: 这是GET-Reqeust的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"
         }
     }
}

The thing is, that I need to have the self-link of "anotherResource". 问题是,我需要具有“ anotherResource”的自链接。 Instead of: 代替:

 "href": "http://localhost:8080/persons/1/anotherResource"

I need to have link something like: 我需要像这样的链接:

"href": "http://localhost:8080/anotherResources/2"

I know that I can implement it by doing additional request. 我知道可以通过执行其他请求来实现它。 But this solution is not practical/possible in my situation, I need to have a lot of data and it's not good to perform an additional request for each item. 但是在我的情况下,此解决方案不切实际/不可能,我需要大量数据,并且对每个项目都执行额外的请求并不好。

Any suggestions/solutions? 有什么建议/解决方案吗?

You can try to use ResourceProcessor with RepositoryEntityLinks to build the link you need: 您可以尝试将ResourceProcessorRepositoryEntityLinks一起使用来构建所需的链接:

@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;
    }
}

But be careful here, because if the resource person does not eagerly have nested anotherResource you can catch the LazyInitializationException (not sure, but check it, please...) or get additional query to DB for every person.getAnotherResource() call ( the N+1 queries issue ). 但是这里要小心,因为如果资源person并不急于嵌套anotherResource你可以赶上LazyInitializationException (不知道,但检查,请...)或获得额外查询数据库,每person.getAnotherResource()调用( 在N + 1个查询问题 )。 That's why it's better to use a relative link like '/persons/1/anotherResource'. 这就是为什么最好使用相对链接,例如“ / persons / 1 / anotherResource”。

Endpoints comments 端点评论

Are you not using the @RequestMapping anotation to declare REST endpoints? 您是否没有使用@RequestMapping注释来声明REST端点?

I would try to define the different endpoints using a different @RequestMapping calls. 我将尝试使用不同的@RequestMapping调用来定义不同的端点。

In this way, the program can have one RequestMapping for /persons where any persons-related actions are associated to its respective endpoint, and define another "root" (mandatory quotes, but you sure get where i am heading to) mapping for /anotherresources where you could add the necessary endpoints. 这样,程序可以为/persons提供一个RequestMapping,其中任何与人相关的动作都与其相应的端点相关联,并为/anotherresources定义另一个“根”(强制引号,但是您肯定会到达我的/anotherresources )映射。您可以在其中添加必要的端点。

Response processing 响应处理

If all you need is to change the JSON that the user receives when accessing a GET endpoint, You could just preprocess the parameter and update the JSON before actually sending it back to the user. 如果您需要的只是更改用户在访问GET端点时收到的JSON,则可以对参数进行预处理并更新JSON,然后再将其实际发送回用户。

But of course, if you rewrite the JSOn without backing the new value for the parameter in the JSON with an existing endpoint, the user will be having trouble when trying to access that URI. 但是,当然,如果您重写JSOn而不用现有端点支持JSON中参数的新值,则用户在尝试访问该URI时会遇到麻烦。 If you only need to form the JSON, I would assume that the endpoints for /anotherresources already exist. 如果仅需要形成JSON,我将假定/anotherresources的端点已经存在。

For detailed info on @RequestMapping, be sure to visit the Spring Docs website . 有关@RequestMapping的详细信息,请确保访问Spring Docs网站

I hope i helped you! 希望我能对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM