Go to https://start.spring.io/ and create initial springboot project
- select Java and Maven
- Add dependencies:
- Lombok - Java annotation library which helps to reduce boiler plate code
- Spring Web (RESTful app using Spring MVC)
- Spring Data MongoDB
- Generate and download the starter project
- open pom.xml file "as a project" using Intellij Idea
-
src/main/java:
This directory houses the primary Java source code for the application. It follows the package structure, mirroring the project's package hierarchy. -
src/main/resources:
Non-Java resources, such as configuration files, static content, and templates, are stored here. Contents are included in the application's classpath. -
src/test/java:
Test classes for unit and integration testing. The structure mirrorssrc/main/java. -
src/test/resources:
Test-specific resources, like configuration files for testing. -
target:
Automatically generated by the build tool, it contains compiled classes, JARs, and other build artifacts. -
pom.xml (Maven) or build.gradle (Gradle):
Project configuration files specifying dependencies, plugins, and build settings. -
.gitignore:
Lists files and directories to be ignored by version control systems like Git. -
mvnw and mvnw.cmd:
Maven wrapper scripts enabling building the project without a separate Maven installation. -
README.md:
Documentation providing an overview of the project, setup instructions, and other essential details.
-
modelVersion: Specifies the POM model version (always set to 4.0.0). -
groupId,artifactId, version: Unique identifiers for your project. The combination of these elements forms the project's globally unique identifier (GAV). -
packaging: Specifies the packaging type (e.g., JAR, WAR) for the project. -
properties: Defines project-wide properties, such as Java version. Centralizing version information helps maintain consistency. -
dependencies: Lists dependencies required for the project. Spring Boot starters (like spring-boot-starter-web) include commonly used dependencies, simplifying configuration. -
build: Configures build-related settings. -
plugins: Specifies Maven plugins. The spring-boot-maven-plugin is essential for packaging the application as an executable JAR or WAR.
- Right click and run DemoApplication.java file
The log message TomcatWebServer: Tomcat started on port 8080 (http) with context path '' indicates the successful startup of the embedded Tomcat server in a Spring Boot application.
-
TomcatWebServer: Refers to the class responsible for managing the embedded Tomcat server within the Spring Boot framework.
-
Tomcat started on port 8080: Signifies that the Tomcat server has been successfully launched and is listening on port 8080. This is a common port for web applications.
-
(http): Denotes the protocol being used, in this case, HTTP.
-
with context path '': Specifies the context path of the web application. An empty context path ('') means the application is directly accessible from the root of the server.
This section provides guidance on addressing the com.mongodb.MongoSocketOpenException: Exception opening socket error when working with MongoDB in your Spring Boot application.
The error indicates that the application encountered difficulties when trying to open a socket connection to the MongoDB server. This can occur for various reasons, including network issues, incorrect server configuration, or MongoDB server unavailability.
- Access MongoDB Atlas.
- Log in or create an account.
- Choose or create a project and a MongoDB Atlas cluster.
- For an existing cluster, access the cluster's control panel and click on its name.
- Navigate to the "Database Access" section to add a user for connecting to the database.
- Click "Add a Database User" or modify an existing user.
- Specify a username and password.
- Ensure that the user has appropriate privileges for the desired database.
- Go to the "Network Access" section.
- Add the allowed IP addresses for connecting to the cluster. You can allow all IP addresses (
0.0.0.0/0) or add specific IP addresses.
- In the cluster's control panel, click "Connect" and choose "Connect Your Application".
- Select "Java" as the language and "3.12 or later" for the MongoDB driver.
- Copy the provided connection URI (
uriorstring).
- In the copied URI, replace
<password>with the created user's password. - You can also replace other values, such as the database name.
Add the following confing in resources/application.yml
spring:
data:
mongodb:
uri: mongodb+srv://alin:alin@cluster0.f4v5k.mongodb.net/?retryWrites=true&w=majority
database: products```Spring Boot MVC (Model-View-Controller) is a software design architecture model that divides an application into three main components: Model, View, and Controller. Spring Boot MVC is an implementation of the MVC architecture within the Spring Boot framework, which is a derivative project of the larger Spring platform.
- Purpose: Represents the data structure of the application.
- Location: Typically found in the
modelpackage.
- Purpose: Handles incoming HTTP requests, interacts with services, and returns appropriate responses.
- Location: Typically found in the
controllerpackage.
- Purpose: Manages database operations for the application's model.
- Location: Typically found in the
repositorypackage.
- Purpose: Contains business logic, interacts with the repository, and provides services to controllers.
- Location: Typically found in the
servicepackage.