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:
-
Layered Architecture:
- Clear separation of
controller
↔service
↔repository
impl
packages for interface implementations
- Clear separation of
-
Testing:
- Unit tests mirror main package structure
- Dedicated
integration
package for IT tests *IT.java
suffix for integration tests (Maven Failsafe convention)
-
MongoDB-Specific:
@Document
entities inmodel/
- Custom queries in
repository/
interfaces MongoConfig
for custom connection pooling
-
Configuration:
- YAML files for environment-specific configs
- Separate
bootstrap.yml
for Spring Cloud Config (if used)
-
Exception Handling:
- Centralized
GlobalExceptionHandler
- Domain-specific exceptions in
exception/
- Centralized
Example Critical Files:
- Main Application Class:
// ProductCatalogApplication.java
@SpringBootApplication
@EnableMongoRepositories
public class ProductCatalogApplication {
public static void main(String[] args) {
SpringApplication.run(ProductCatalogApplication.class, args);
}
}
- MongoDB Configuration:
// MongoConfig.java
@Configuration
public class MongoConfig {
@Bean
public MongoCustomConversions customConversions() {
return new MongoCustomConversions(List.of(
new ProductToDocumentConverter(),
new DocumentToProductConverter()
));
}
}
- 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?