Overview
sgivu-client manages client information (persons and companies) within the SGIVU ecosystem. It provides CRUD operations, advanced search, and counters, exposing a REST API protected by roles and permissions issued by the Authorization Server (sgivu-auth).
Technologies
- Java: 21
- Spring Boot: 4.0.1
- Spring Security: Resource Server (JWT validation)
- Spring Data JPA: PostgreSQL persistence
- Flyway: Database migrations in
src/main/resources/db/migration - Spring Boot Actuator: Health monitoring
- Micrometer Tracing: Zipkin integration
- SpringDoc OpenAPI: API documentation
- MapStruct: DTO mapping
- Lombok: Code generation
Port Configuration
- Default Port: 8082 (configurable via
PORTor config server)
Prerequisites
- JDK 21
- Maven 3.9+
- PostgreSQL database
sgivu-configandsgivu-discoveryservices running
Running the Service
Development with Docker Compose
Local Execution
Docker Build
Client Types
Person
Individual clients with personal information:- First name, last name
- Document number (national ID, passport, etc.)
- Date of birth
- Email, phone
- Addresses
Company
Business clients with corporate information:- Legal name
- Tax ID (TIN/EIN)
- Industry sector
- Email, phone
- Addresses
Security
Authentication
JWT Token Validation:sgivu-clientacts as Resource Server- Validates tokens issued by
sgivu-auth - Token signature verified via JWKS endpoint
Authorization
Required Permissions:person:create- Create person clientsperson:read- View person clientsperson:update- Update person clientsperson:delete- Delete person clientscompany:create- Create company clientscompany:read- View company clientscompany:update- Update company clientscompany:delete- Delete company clients
@PreAuthorize annotations in PersonController and CompanyController
Gateway or clients must send valid Bearer token with required authorities. Token must be issued by
sgivu-auth.Database Schema
Flyway Migrations
Location:src/main/resources/db/migration/V1__initial_schema.sql
Tables Created:
- clients
- persons
- companies
- addresses
Base client table with discriminator:
id: Primary keyclient_type: ENUM (PERSON, COMPANY)email,phone: Contact informationcreated_at,updated_at: Audit timestamps
persons.document_number(unique)companies.tax_id(unique)clients.emailaddresses.client_id
Configuration
Database connection configured insgivu-config-repo/sgivu-client-*.yml:
API Endpoints
Person Management
List persons with advanced searchRequired Permission:
person:readQuery Parameters:documentNumber: Filter by documentfirstName,lastName: Search by nameemail,phone: Contact searchpage,size,sort: Pagination
Get person by ID including addressesRequired Permission:
person:readCount total personsRequired Permission:
person:readCreate new personRequired Permission:
person:createValidations:- Unique document number
- Valid email format
- Date of birth in the past
Update person informationRequired Permission:
person:updateDelete person (soft delete)Required Permission:
person:deleteCompany Management
List companies with filteringRequired Permission:
company:readQuery Parameters:taxId: Filter by tax IDlegalName: Search by nameindustrySector: Filter by industryemail,phone: Contact searchpage,size,sort: Pagination
Get company by ID including addressesRequired Permission:
company:readCount total companiesRequired Permission:
company:readCreate new companyRequired Permission:
company:createValidations:- Unique tax ID
- Valid email format
- Required legal name
Update company informationRequired Permission:
company:updateDelete company (soft delete)Required Permission:
company:deleteAddress Management
Add address to clientRequired Permission:
person:update or company:updateUpdate client addressRequired Permission:
person:update or company:updateDelete client addressRequired Permission:
person:delete or company:deleteRequest/Response Examples
Create Person
Response
Create Company
Observability
Actuator Endpoints
/actuator/health: Service health with database connectivity/actuator/info: Application metadata and version
Distributed Tracing
Zipkin Integration:- Endpoint:
http://sgivu-zipkin:9411 - Automatic trace correlation
- Span creation for database operations
API Documentation
Access via gateway:http://gateway:8080/docs/client/swagger-ui/index.html
Testing
- Unit tests for services and mappers
- Repository integration tests with test containers
- Controller tests with MockMvc
- Security authorization tests
Troubleshooting
401/403 Authorization errors
401/403 Authorization errors
Database connection errors
Database connection errors
Symptoms: Service fails to start or database operations failSolutions:
- Verify
DEV_CLIENT_DB_*orPROD_CLIENT_DB_*environment variables - Check PostgreSQL is running and accessible
- Ensure database and user exist
- Review Flyway migration status
- Check database credentials
Flyway migration failures
Flyway migration failures
Symptoms: Service fails to start with Flyway errorSolutions:
- Check migration script syntax
- Verify database user has CREATE/ALTER permissions
- Review
flyway_schema_historytable - Clear failed migrations if in development
- Ensure migrations are sequential (V1, V2, etc.)
Duplicate document/tax ID error
Duplicate document/tax ID error
Symptoms: Creation fails with unique constraint violationSolutions:
- Check if client already exists before creation
- Handle duplicate key exceptions gracefully
- Return clear error message to user
- Consider soft delete for deactivated clients
- Allow searching by document before creation
Service not appearing in Eureka
Service not appearing in Eureka
Symptoms: Client service not visible in discovery dashboardSolutions:
- Verify
eureka.client.service-url.defaultZoneconfiguration - Check network connectivity to
sgivu-discovery - Ensure
spring.application.nameis set correctly - Review Eureka registration logs
- Wait 30-60 seconds for registration to complete
Environment Variables
| Variable | Description | Example |
|---|---|---|
PORT | Server port | 8082 |
DB_HOST | PostgreSQL host | sgivu-postgres-client |
DB_PORT | PostgreSQL port | 5432 |
DB_NAME | Database name | sgivu_client |
DB_USERNAME | Database username | sgivu_user |
DB_PASSWORD | Database password | secure_password |
DEV_CLIENT_DB_* | Dev database variables | Prefixed with DEV_CLIENT_DB_ |
PROD_CLIENT_DB_* | Prod database variables | Prefixed with PROD_CLIENT_DB_ |
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI | Auth server issuer | http://sgivu-auth:9000 |
Best Practices
Validation
Validate unique identifiers (document, tax ID) before creation
Soft Delete
Use soft deletes to maintain referential integrity
Search Optimization
Index frequently searched fields for performance
Audit Trail
Track creation and update timestamps for all entities
Related Services
Purchase-Sale
References clients as buyers and sellers
User Service
Similar entity management patterns
Gateway
Routes client management requests
Auth Server
Issues tokens for authorization