deprecated‐2. Product Catalog Service‐MongoDB Edition‐ Project Structure - Wiz-DevTech/prettygirllz GitHub Wiki

Here's the optimized Maven project structure for your Java Product Catalog Service with MongoDB, following industry best practices:

product-service/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── productcatalog/
│   │   │           ├── config/                # Configuration classes
│   │   │           │   ├── MongoConfig.java
│   │   │           │   └── SwaggerConfig.java
│   │   │           ├── controller/            # API endpoints
│   │   │           │   ├── ProductController.java
│   │   │           │   ├── CandyController.java
│   │   │           │   └── FashionController.java
│   │   │           ├── model/                 # MongoDB entities
│   │   │           │   ├── Product.java
│   │   │           │   ├── SKU.java
│   │   │           │   └── ColorVariant.java
│   │   │           ├── repository/            # MongoDB repositories
│   │   │           │   ├── ProductRepository.java
│   │   │           │   └── SKURepository.java
│   │   │           ├── service/               # Business logic
│   │   │           │   ├── ProductService.java
│   │   │           │   ├── impl/              # Service implementations
│   │   │           │   │   ├── ProductServiceImpl.java
│   │   │           │   │   └── InventoryServiceImpl.java
│   │   │           ├── exception/             # Custom exceptions
│   │   │           │   ├── InvalidSKUException.java
│   │   │           │   └── GlobalExceptionHandler.java
│   │   │           └── ProductCatalogApplication.java  # Main class
│   │   └── resources/
│   │       ├── application.yml                # Configs (YAML format)
│   │       ├── bootstrap.yml
│   │       └── messages.properties           # I18n
│   └── test/                                 # Mirror main structure
│       └── java/
│           └── com/
│               └── productcatalog/
│                   ├── controller/
│                   │   └── ProductControllerTest.java
│                   ├── service/
│                   │   └── ProductServiceTest.java
│                   └── integration/
│                       └── ProductRepositoryIT.java
├── target/                                   # Generated builds
├── pom.xml                                  # Maven config
├── Dockerfile
└── README.md

Key Structure Decisions:

  1. Layered Architecture:

    • Clear separation of controllerservicerepository
    • impl packages for interface implementations
  2. Testing:

    • Unit tests mirror main package structure
    • Dedicated integration package for IT tests
    • *IT.java suffix for integration tests (Maven Failsafe convention)
  3. MongoDB-Specific:

    • @Document entities in model/
    • Custom queries in repository/ interfaces
    • MongoConfig for custom connection pooling
  4. Configuration:

    • YAML files for environment-specific configs
    • Separate bootstrap.yml for Spring Cloud Config (if used)
  5. Exception Handling:

    • Centralized GlobalExceptionHandler
    • Domain-specific exceptions in exception/

Example Critical Files:

  1. Main Application Class:
// ProductCatalogApplication.java
@SpringBootApplication
@EnableMongoRepositories
public class ProductCatalogApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductCatalogApplication.class, args);
    }
}
  1. MongoDB Configuration:
// MongoConfig.java
@Configuration
public class MongoConfig {
    @Bean
    public MongoCustomConversions customConversions() {
        return new MongoCustomConversions(List.of(
            new ProductToDocumentConverter(),
            new DocumentToProductConverter()
        ));
    }
}
  1. Integration Test:
// ProductRepositoryIT.java
@DataMongoTest
@Import(MongoConfig.class)
class ProductRepositoryIT {
    @Autowired
    private ProductRepository repo;
    
    @Test
    void shouldSaveProductWithCustomConverters() {
        Product p = new Product("ABC-1234", "Test", 10);
        assertNotNull(repo.save(p).getId());
    }
}

This structure ensures: ✅ Testability - Clear separation of unit vs integration tests
Maintainability - Logical grouping by domain (candy/fashion modules)
Extensibility - Ready for Docker/Kubernetes deployment
MongoDB Best Practices - Custom converters and indexed entities

Would you like me to add any specific:

  • CI/CD pipeline structure?
  • Microservice communication examples?
  • Detailed Docker/Helm configurations?