Skip to content

alingrig87/springboot-mongo-products-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot and MongoDB project

SpringBoot project creation

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 the project in Intellij

- open pom.xml file "as a project" using Intellij Idea

SpringBoot Project Structure

  • 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 mirrors src/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.

pom.xml structure

  • 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.

Start the app

  • Right click and run DemoApplication.java file

Log Message Explanation

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.

Handling MongoSocketOpenException

This section provides guidance on addressing the com.mongodb.MongoSocketOpenException: Exception opening socket error when working with MongoDB in your Spring Boot application.

Error Explanation

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.

Connecting Spring Boot to MongoDB Atlas

Steps to Obtain MongoDB Atlas Connection URI

1. Log in to MongoDB Atlas:
2. Select or Create a Project and Cluster:
  • 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.
3. Access "Database Access" Section:
  • Navigate to the "Database Access" section to add a user for connecting to the database.
4. Add or Configure the User:
  • 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.
5. Configure Allowed IP Addresses in "Network Access":
  • 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.
6. Obtain the Connection URI:
  • 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 (uri or string).
7. Replace Sensitive Data:
  • 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

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.

Model

  • Purpose: Represents the data structure of the application.
  • Location: Typically found in the model package.

Controller

  • Purpose: Handles incoming HTTP requests, interacts with services, and returns appropriate responses.
  • Location: Typically found in the controller package.

Repository

  • Purpose: Manages database operations for the application's model.
  • Location: Typically found in the repository package.

Service

  • Purpose: Contains business logic, interacts with the repository, and provides services to controllers.
  • Location: Typically found in the service package.

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors