-
Notifications
You must be signed in to change notification settings - Fork 5
feat(java): set up backend scaffolding with basic CRUD #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9536cde
feat: set up repo structure and backend connection
cbullinger f55b1d6
feat: add model layer
cbullinger 7e25be6
feat: add CRUD endpoints
cbullinger dd8fe3a
fix: remove unneeded files and update .gitignore and .gitattributes
cbullinger 7ea6b1e
fix: resolve module access issue with driver v5.6
cbullinger 462d71f
refactor(RuntimeException): Add new DatabaseOperationException.java c…
cbullinger 170c3c5
refactor(pojo): apply patch and use POJO codec
cbullinger 2ea3ff0
refactor(serializer): Apply patch and use ObjectMapper
cbullinger f454cc8
refactor(field names): implement nested fields consistently
cbullinger c6eeb46
test: add new tests based on refactors
cbullinger 2d5087b
refactor(DTOs): create missing return types
cbullinger 7b9029c
refactor(spring data): implement spring data fully
cbullinger 360d825
update TODOs
cbullinger 489c950
rename app
cbullinger 5b6f266
remove unnecessary file
cbullinger 605d839
update README
cbullinger c23c709
Apply Vitaliy feedback
cbullinger 0f3dc54
Apply suggestion from @cbullinger
cbullinger 9253973
Apply suggestion from @cbullinger
cbullinger cc13e06
docs: update comments throughout for consistency
cbullinger 6155906
docs: update readme and remove internal todo doc
cbullinger b10f743
fix(movies): add back missing @Document tag
cbullinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Git Attributes for docs-sample-apps | ||
| # Marks auto-generated files that are committed to version control | ||
| # so they're collapsed in GitHub PR diffs and excluded from code review | ||
|
|
||
| # Maven Wrapper - Auto-generated by Apache Maven (committed to git) | ||
| # See: https://maven.apache.org/wrapper/ | ||
| server/java-spring/mvnw linguist-generated=true | ||
| server/java-spring/mvnw.cmd linguist-generated=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # ============================================================================= | ||
| # Root .gitignore for MongoDB Sample Apps Monorepo | ||
| # ============================================================================= | ||
| # This file contains patterns common to all projects in the monorepo. | ||
| # Backend-specific patterns are defined in each backend's .gitignore file. | ||
| # ============================================================================= | ||
|
|
||
| # Environment Variables (Global) | ||
| .env | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
| !.env.example | ||
|
|
||
| # Operating System Files | ||
| .DS_Store | ||
| Thumbs.db | ||
|
|
||
| # IDE and Editor Files (Global) | ||
| .idea/ | ||
| .vscode/ | ||
| *.swp | ||
| *.swo | ||
| *.swn | ||
| *.bak | ||
| *.tmp | ||
| *.iml | ||
| .project | ||
| .classpath | ||
| .settings/ | ||
| *.sublime-project | ||
| *.sublime-workspace | ||
|
|
||
| # Logs (Global) | ||
| logs/ | ||
| *.log | ||
|
|
||
| # Temporary Files (Global) | ||
| *.tmp | ||
| *.temp | ||
| .cache/ | ||
|
|
||
| # Node.js (Global - applies to all Node.js projects) | ||
| node_modules/ | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| .pnpm-debug.log* | ||
|
|
||
| # Test Coverage (Global) | ||
| coverage/ | ||
| *.lcov | ||
| .nyc_output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,4 +23,5 @@ coverage/ | |
| .npm | ||
|
|
||
| # macOS | ||
| .DS_Store | ||
| .DS_Store | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # MongoDB Connection | ||
| # Replace with your MongoDB Atlas connection string or local MongoDB URI | ||
| MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/?retryWrites=true&w=majority | ||
|
|
||
| # Server Configuration | ||
| # Port on which the Spring Boot application will run | ||
| PORT=3001 | ||
|
|
||
| # CORS Configuration | ||
| # Allowed origin for cross-origin requests (frontend URL) | ||
| # For multiple origins, separate with commas | ||
| CORS_ORIGIN=http://localhost:3000 | ||
|
|
cbullinger marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Java/Spring Boot - Specific Ignores | ||
| # Common patterns | ||
| .env | ||
| .DS_Store | ||
|
|
||
| #Cache | ||
| .cache/ | ||
|
|
||
| # Compiled Files | ||
| *.class | ||
| *.ctxt | ||
| .mtj.tmp/ | ||
|
|
||
| # Package Files | ||
| *.jar | ||
| *.war | ||
| *.nar | ||
| *.ear | ||
|
|
||
| # Crash Logs | ||
| hs_err_pid* | ||
| replay_pid* | ||
|
|
||
| # Maven | ||
| target/ | ||
| pom.xml.tag | ||
| pom.xml.releaseBackup | ||
| pom.xml.versionsBackup | ||
| pom.xml.next | ||
| release.properties | ||
| dependency-reduced-pom.xml | ||
| buildNumber.properties | ||
| .mvn/timing.properties | ||
| .mvn/wrapper/maven-wrapper.jar | ||
|
|
||
| # IntelliJ IDEA | ||
| *.iws | ||
| *.iml | ||
| *.ipr | ||
| out/ | ||
| !**/src/main/**/out/ | ||
| !**/src/test/**/out/ | ||
|
|
||
| # Eclipse | ||
| .apt_generated | ||
| .classpath | ||
| .factorypath | ||
| .project | ||
| .settings | ||
| .springBeans | ||
| .sts4-cache | ||
| bin/ | ||
| !**/src/main/**/bin/ | ||
| !**/src/test/**/bin/ | ||
|
|
||
| # NetBeans | ||
| /nbproject/private/ | ||
| /nbbuild/ | ||
| /dist/ | ||
| /nbdist/ | ||
| /.nb-gradle/ | ||
|
|
||
| # Spring Boot | ||
| spring-boot-devtools.properties |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Maven Wrapper Configuration | ||
| # Use ./mvnw instead of mvn to automatically download and use the specified Maven version. | ||
|
|
||
| # Maven Wrapper version (not the Maven version itself) | ||
| wrapperVersion=3.3.4 | ||
|
|
||
| # Download only Maven binaries (recommended for most projects) | ||
| distributionType=only-script | ||
|
|
||
| # Maven version to download - pins everyone to Maven 3.8.6 for consistent builds | ||
| distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.6/apache-maven-3.8.6-bin.zip | ||
|
|
||
| # Usage: ./mvnw clean install (Unix/macOS) or mvnw.cmd clean install (Windows) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| # sample-app-java-mflix (INTERNAL) | ||
|
|
||
| A Spring Boot REST API demonstrating MongoDB CRUD operations using Spring Data MongoDB with the sample_mflix database. | ||
|
|
||
| ## Overview | ||
|
|
||
| This application provides a REST API for managing movie data from MongoDB's sample_mflix database. It demonstrates: | ||
|
|
||
| - Spring Data MongoDB for simplified data access | ||
| - CRUD operations (Create, Read, Update, Delete) | ||
| - Text search functionality | ||
| - Filtering, sorting, and pagination | ||
| - Comprehensive error handling | ||
| - API documentation with Swagger/OpenAPI | ||
| - MongoTemplate for complex queries | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Java 21 or later | ||
| - Maven 3.6 or later | ||
| - MongoDB Atlas account or local MongoDB instance with sample_mflix database | ||
|
|
||
| ## Project Structure | ||
|
|
||
| ``` | ||
| server/java-spring/ | ||
| ├── src/ | ||
| │ ├── main/ | ||
| │ │ ├── java/com/mongodb/samplemflix/ | ||
| │ │ │ ├── SampleMflixApplication.java # Main application class | ||
| │ │ │ ├── config/ # Configuration classes | ||
| │ │ │ │ ├── MongoConfig.java # MongoDB client configuration | ||
| │ │ │ │ ├── CorsConfig.java # CORS configuration | ||
| │ │ │ │ └── DatabaseVerification.java # Startup database verification | ||
| │ │ │ ├── controller/ # REST controllers | ||
| │ │ │ ├── service/ # Business logic layer | ||
| │ │ │ ├── repository/ # Data access layer | ||
| │ │ │ ├── model/ # Domain models and DTOs | ||
| │ │ │ ├── exception/ # Custom exceptions | ||
| │ │ │ └── util/ # Utility classes | ||
| │ │ └── resources/ | ||
| │ │ └── application.properties # Application configuration | ||
| │ └── test/ # Test classes | ||
| ├── pom.xml # Maven dependencies | ||
| └── README.md | ||
| ``` | ||
|
|
||
| ## Setup Instructions | ||
|
|
||
| ### 1. Clone the Repository | ||
|
|
||
| ```bash | ||
| git clone <repository-url> | ||
| cd server/java-spring | ||
| ``` | ||
|
|
||
| ### 2. Configure Environment Variables | ||
|
|
||
| Copy the example environment file and update with your MongoDB connection details: | ||
|
|
||
| ```bash | ||
| cp .env.example .env | ||
| ``` | ||
|
|
||
| Edit `.env` and set your MongoDB connection string: | ||
|
|
||
| ```properties | ||
| MONGODB_URI=mongodb+srv://username:password@cluster.mongodb.net/?retryWrites=true&w=majority | ||
| PORT=3001 | ||
| CORS_ORIGIN=http://localhost:3000 | ||
| ``` | ||
|
|
||
| > **Note**: This project uses [spring-dotenv](https://github.com/paulschwarz/spring-dotenv) to automatically load `.env` files, similar to Node.js applications. The `.env` file will be loaded automatically when you run the application. | ||
|
|
||
| ### 3. Load Sample Data | ||
|
|
||
| If you haven't already, load the `sample_mflix` database into your MongoDB instance: | ||
|
|
||
| - **MongoDB Atlas**: Use the "Load Sample Dataset" option in your cluster | ||
| - **Local MongoDB**: Follow the [MongoDB sample data documentation](https://www.mongodb.com/docs/atlas/sample-data/) | ||
|
|
||
| ### 4. Build the Project | ||
|
|
||
| ```bash | ||
| mvn clean install | ||
| ``` | ||
|
|
||
| ### 5. Run the Application | ||
|
|
||
| ```bash | ||
| mvn spring-boot:run | ||
| ``` | ||
|
|
||
| The application will start on `http://localhost:3001` (or the port specified in your `.env` file). | ||
|
|
||
| ## API Documentation | ||
|
|
||
| Once the application is running, you can access: | ||
|
|
||
| - **Swagger UI**: http://localhost:3001/swagger-ui.html | ||
| - **OpenAPI JSON**: http://localhost:3001/api-docs | ||
|
|
||
| ## API Endpoints | ||
|
|
||
| ### Movies (✅ Implemented) | ||
|
|
||
| - `GET /api/movies` - Get all movies (with filtering, sorting, pagination) | ||
| - `GET /api/movies/{id}` - Get a single movie by ID | ||
| - `POST /api/movies` - Create a new movie | ||
| - `POST /api/movies/batch` - Create multiple movies | ||
| - `PUT /api/movies/{id}` - Update a movie | ||
| - `PATCH /api/movies` - Update multiple movies | ||
| - `DELETE /api/movies/{id}` - Delete a movie | ||
| - `DELETE /api/movies` - Delete multiple movies | ||
| - `DELETE /api/movies/{id}/find-and-delete` - Find and delete a movie | ||
|
|
||
| ## Development | ||
|
|
||
| ### Running Tests | ||
|
|
||
| ```bash | ||
| # Run all tests | ||
| mvn test | ||
|
|
||
| # Run tests with coverage | ||
| mvn test jacoco:report | ||
| ``` | ||
|
|
||
| ### Building for Production | ||
|
|
||
| ```bash | ||
| mvn clean package | ||
| java -jar target/sample-mflix-spring-1.0.0.jar | ||
| ``` | ||
|
|
||
| ## Implementation Status | ||
|
|
||
| ### Completed Features | ||
|
|
||
| - **Movies CRUD API** - Full create, read, update, delete operations | ||
| - **Spring Data MongoDB** - Repository pattern with MongoTemplate for complex queries | ||
| - **Text Search** - Full-text search on movie titles, plots, and genres | ||
| - **Filtering & Pagination** - Query parameters for filtering, sorting, and pagination | ||
| - **Custom Exception Handling** - Global exception handler with proper HTTP status codes | ||
| - **Type-Safe DTOs** - Specific response types instead of generic Maps | ||
| - **Unit Tests** - 35 tests covering service and controller layers | ||
| - **OpenAPI Documentation** - Swagger UI available at `/swagger-ui.html` | ||
| - **Database Verification** - Startup checks for database connectivity and indexes | ||
|
|
||
|
|
||
| ## Technology Stack | ||
|
|
||
| - **Framework**: Spring Boot 3.5.7 | ||
| - **Java Version**: 21 | ||
| - **MongoDB**: Spring Data MongoDB 4.5.5 | ||
| - **Build Tool**: Maven | ||
| - **API Documentation**: SpringDoc OpenAPI 2.8.13 | ||
| - **Testing**: JUnit 5, Mockito, Spring Boot Test | ||
|
|
||
| ## Educational Purpose | ||
|
|
||
| This application is designed as an educational sample to demonstrate: | ||
|
|
||
| 1. How to use Spring Data MongoDB for simplified data access | ||
| 2. Best practices for Spring Boot REST API development | ||
| 3. Proper separation of concerns (Controller → Service → Repository) | ||
| 4. MongoDB CRUD operations and query patterns | ||
| 5. Error handling and validation in Spring Boot | ||
| 6. Using MongoTemplate for complex queries alongside Spring Data repositories | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### Connection Issues | ||
|
|
||
| If you encounter connection issues: | ||
|
|
||
| 1. Verify your `MONGODB_URI` is correct | ||
| 2. Check that your IP address is whitelisted in MongoDB Atlas | ||
| 3. Ensure the sample_mflix database exists and contains data | ||
| 4. Check the application logs for detailed error messages | ||
|
|
||
| ### Build Issues | ||
|
|
||
| If Maven build fails: | ||
|
|
||
| 1. Ensure you have Java 21 or later installed: `java -version` | ||
| 2. Ensure Maven is installed: `mvn -version` | ||
| 3. Clear Maven cache: `mvn clean` | ||
| 4. Try rebuilding: `mvn clean install` | ||
|
|
||
| ## License | ||
|
|
||
| [TBD] | ||
|
|
||
| ## Contributing | ||
|
|
||
| [TBD] | ||
|
|
||
| ## Issues | ||
|
|
||
| [TBD] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.