2.2 KiB
2.2 KiB
| description |
|---|
| The standard workflow for creating or modifying Groovy REST API endpoints in this project. |
This workflow ensures all API development adheres to the project's established, stable patterns to prevent bugs and maintain consistency.
Key Reference Documents
PRIMARY REFERENCE: /docs/solution-architecture.md — Comprehensive solution architecture and API design standards
SUPPORTING REFERENCES:
- Current ADRs in
/docs/adr/(skip/docs/adr/archive/- consolidated in solution-architecture.md) - Working examples:
src/com/umig/api/v2/TeamsApi.groovy
-
Analyze the Existing Pattern:
- Before writing any code, thoroughly review a working, stable API file like
src/com/umig/api/v2/TeamsApi.groovy. - Pay close attention to the structure: separate endpoint definitions for each HTTP method, simple
try-catchblocks for error handling, and standardjavax.ws.rs.core.Responseobjects.
- Before writing any code, thoroughly review a working, stable API file like
-
Replicate the Pattern:
- Create a new endpoint definition for each HTTP method (
GET,POST,PUT,DELETE). - Do NOT use a central dispatcher, custom exception classes, or complex helper methods for error handling. Keep all logic within the endpoint method.
- Create a new endpoint definition for each HTTP method (
-
Implement Business Logic:
- Write the core business logic inside a
tryblock. - Call the appropriate
UserRepositoryorTeamRepositorymethods.
- Write the core business logic inside a
-
Handle Success Cases:
- For
GET,POST, andPUT, return aResponse.ok()orResponse.status(Response.Status.CREATED)with aJsonBuilderpayload. - CRITICAL: For a successful
DELETE, always returnResponse.noContent().build(). Do NOT attempt to return a body.
- For
-
Handle Error Cases:
- Use
catch (SQLException e)to handle specific database errors (e.g., foreign key violations23503, unique constraint violations23505). - Use a generic
catch (Exception e)for all other unexpected errors. - In all
catchblocks, log the error usinglog.error()orlog.warn()and return an appropriateResponse.status(...)with a simple JSON error message.
- Use
-
Validate Inputs:
- Strictly validate all incoming data (path parameters, request bodies) at the beginning of the endpoint method.
- Return a
400 Bad Requestfor any invalid input.