Environment: REST API (Spring MVC) + SPA (AngularJS)
Let's assume I have two resources available via the following URIs, respectively.
/resource/1 -> Resource #1
/resource/2 -> Resource #2
My application business logic allows me to merge one resource into another. Let's say I merge Resource #1 into Resource #2. After doing so, any future requests to Resource #1 should return Resource #2. The same would apply to any child resources, for instance, if I have
/resource/1/child-resource -> Child Resource #1
/resource/2/child-resource -> Child Resource #2
then any requests to Child Resource #1 should return Child Resource #2 following the merge. This is not only for representations of this information in the client. For all intents and purposes, Resource #1 is gone and any references to it in the application should return Resource #2.
If this were a typical HTTP request, sending a 301 redirect response the location header reflecting the new URI to the resource which would force the client (browser) to redirect to that URI is clear. What I am not certain about is how to do this properly when using an SPA and AJAX requests, namely
- whether the controller should internally resolve the proper ID for the requested resource and return that resource in one go, or
- should it return a 301 response as described above and the frontend code would have to deal with handling the redirect (which should not reload the page).
What is the proper way to respond to such requests given this business logic?