GithubHelp home page GithubHelp logo

hardik-sharma-08 / spring-boot Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 76 KB

Spring Boot project, including core folders for basic concepts, REST API, and database integration. It also discusses essential Spring concepts and implements robust exception handling and validation for the REST API.

Java 100.00%
exception-handling java spring-boot springdata-jpa springvalidations

spring-boot's Introduction

๐Ÿƒ Spring Boot

This repository is aimed at providing a comprehensive introduction to Spring Boot, particularly focusing on three main aspects: basic Spring Boot concepts, building REST APIs, and integrating with a database using Spring Data JPA.

๐Ÿ“ Explanation of Spring Concepts

  • Spring Beans:

    • Objects managed by Spring IoC container.
    • Core building blocks of a Spring application.
    • Configured with metadata supplied to the container.
    • Typically Java objects.
  • Spring Profiles:

    • Segregate parts of application configuration.
    • Available only in specific environments.
    • Enable different configurations for development, testing, production, etc.
    • Control which beans are loaded and which configurations are applied.
  • Qualifiers:

    • Resolve ambiguity when multiple beans of the same type are available.
    • Used with @Qualifier annotation.
    • Specify which bean should be autowired into a particular field or parameter.
    • Useful when multiple beans implement the same interface or are of the same type.

๐Ÿ“‚ Folder Structure

project/
โ”‚
โ”œโ”€โ”€ demo/
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ main/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ java/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ com.example.demo/
โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ resources/
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ ...
โ”‚   โ”‚   โ””โ”€โ”€ test/
โ”‚   โ”‚       โ””โ”€โ”€ ...
โ”‚   โ””โ”€โ”€ pom.xml
โ”‚
โ”œโ”€โ”€ rest/
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ main/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ java/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ com.example.rest/
โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ resources/
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ ...
โ”‚   โ”‚   โ””โ”€โ”€ test/
โ”‚   โ”‚       โ””โ”€โ”€ ...
โ”‚   โ””โ”€โ”€ pom.xml
โ”‚
โ””โ”€โ”€ rest_database/
    โ”œโ”€โ”€ src/
    โ”‚   โ”œโ”€โ”€ main/
    โ”‚   โ”‚   โ”œโ”€โ”€ java/
    โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ com.example.rest_database/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ students/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ controller/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ mapper/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ response/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ dto/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ repository/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ service/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ studentProfile/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ controller/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ mapper/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ response/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ dto/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ repository/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ service/
    โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ School/
    โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ controller/
    โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ mapper/
    โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ response/
    โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ dto/
    โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ repository/
    โ”‚   โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ service/
    โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ...
    โ”‚   โ”‚   โ””โ”€โ”€ resources/
    โ”‚   โ”‚       โ””โ”€โ”€ ...
    โ”‚   โ””โ”€โ”€ test/
    โ”‚       โ””โ”€โ”€ ...
    โ””โ”€โ”€ pom.xml

The project is organized into three main folders:

  1. ๐Ÿ“ demo: Basic Spring Boot concepts are covered in this folder including Spring Beans, Qualifier, Spring Profiles, Dependency Injection, and creating the first class and service.

  2. ๐Ÿ“ rest: This folder covers the basics of building REST APIs with Spring Boot. It includes creating the first controller using @RestController, and utilizing @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping. Path variables and request parameters are also demonstrated, along with creating the first record.

  3. ๐Ÿ“ rest_database: This folder focuses on integrating Spring Boot with a database using Spring Data JPA. Notably, the application.properties file is updated to application.yaml to configure the database connection. Below is the configuration explained:

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: qwerty123
    driver-class-name: org.postgresql.Driver
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format_sql: true
    database: postgresql
    database-platform: org.hibernate.dialect.PostgresSQLDialect

Explanation of Configuration

  • Datasource Configuration: Specifies the URL, username, password, and driver class for connecting to the PostgreSQL database.
  • JPA Configuration: Configures Hibernate behavior, including DDL auto, SQL logging, and database dialect.

๐Ÿ“ Package Structure for rest_database

In each of the students, studentProfile, and School packages within the rest_database folder, the following components are created:

  • Controller: Defines endpoints for handling HTTP requests.
  • Mapper: Converts between DTOs (Data Transfer Objects) and domain objects.
  • Response: Defines response structures for API endpoints.
  • DTO: Data Transfer Objects representing the data exchanged between the client and server.
  • Repository: Interfaces defining methods for interacting with the database.
  • Service: Implements business logic and interacts with repositories.

This structure helps in maintaining a clean and organized codebase, following the principles of separation of concerns and modularity.

โŒ Exception Handling & Validation

In the rest_database module, exception handling and validation are implemented to enhance the robustness and reliability of the REST API endpoints.

Exception Handling

  • Exception handling is crucial for handling errors and exceptions gracefully in a Spring Boot application.
  • In the controller layer, exception handling is implemented using @ControllerAdvice and @ExceptionHandler annotations.
  • Custom exception classes can be created to represent different types of errors that may occur during the execution of API endpoints.
  • Global exception handlers catch exceptions thrown during request processing and return appropriate HTTP responses with error details.
  • This ensures consistent error responses across all endpoints and improves the overall user experience.

Validation

  • Input validation is important for ensuring that the data sent to the API endpoints is valid and meets the required criteria.
  • In the studentDTO layer, input validation is performed using annotations such as @Valid and @Validated.
  • Bean validation annotations like @NotBlank, @NotNull, @Size, etc., are used to define validation rules for request parameters, request bodies, and path variables.
  • If the incoming data fails validation, Spring Boot automatically detects the validation errors and returns appropriate error responses with details about the validation failures.
  • This helps in preventing invalid data from being processed further, improving data integrity and API reliability.

spring-boot's People

Contributors

hardik-sharma-08 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.