A
A
APsivenko2017-01-08 14:27:56
Java
APsivenko, 2017-01-08 14:27:56

What is the correct way to search for nested REST resources?

Let's say I have a link:
/api/v1/m/projects/{p_id}/sprints/{s_id}/tasks/{t_id}
All CRUD operations are supported.
But how to properly handle the absence of parent resources? And will it be correct to send an error message in response if the parent resource is not found.
For example:

  1. Project not found - error, project not found.
  2. Project found, sprint not found - error, sprint not found.

I have only one solution so far - in the service, look for resources in turn:
  1. Find a project by id.
  2. Find a sprint by id and project.
  3. Find a task by id, project and sprint. (Well, or sprint and id will be enough).
UPDATE
I meant task search. I need to go through the parent resources and check if they are there.
For example:
The "Create Task" method does in turn:
  1. Checking for a project by id. If not, an exception.
  2. Checking for a sprint by id and project. If not, an exception.
  3. Creating a job with a given sprint.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
Zakharov Alexander, 2017-01-08
@AlexZaharow

Such checks should be done by filters. Before calling the business logic.

R
Ruslan Lopatin, 2017-01-08
@lorus

1. The service throws an exception of type SprintNotFoundException, ProjectNotFoundException, TaskNotFoundException
2. The exception handler (global or for the controller) throws a 404 error with details in the response body.
Although an exception of type NotFoundException is usually sufficient. Details (what exactly is not found) are contained in the message of this exception.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question