Spring Boot is an open-source Java-based framework developed by Pivotal (now part of VMware) that makes it easy to create standalone, production-grade Spring applications with minimal configuration
spring boot is a utility project which aims to make it easy to build spring-based, production-ready applications and services with minimum fuss. it provides the shortest way to get a spring web application up and running with the smallest line of code/configuration out-of-the-box.
✅ Key Features:
Feature | Description |
---|---|
Auto-Configuration | Automatically configures Spring and third-party libraries. |
Standalone | No need for external servers like Tomcat or Jetty — it's embedded. |
Production Ready | Comes with features like health checks, metrics, logging, etc. |
Opinionated Defaults | Provides default configurations to speed up development. |
Spring Boot Starter | Predefined dependency groups (e.g., spring-boot-starter-web , etc.) |
Actuator (optional) | Provides monitoring endpoints like /actuator/health , /metrics , etc. |
DevTools (optional) | Enables auto-reload and hot swapping during development. |
✅ Difference Between @Controller
and @RestController
in Spring Boot
Both @Controller
and @RestController
are used to define Spring MVC controllers, but they serve different purposes depending on the response type expected by the client.
Feature | @Controller | @RestController |
---|---|---|
Purpose | Used for returning web pages (views) like JSP, HTML, Thymeleaf | Used for creating RESTful web services / APIs |
Returns | Usually returns a View (String → view name) | Returns JSON or XML response (data) |
Annotation Type | It is a generic controller | It is a specialized version of @Controller |
Requires @ResponseBody | ✅ Yes, you must add @ResponseBody to methods to return JSON/XML | ❌ No, it is included by default |
Use Case | For web applications with frontend UI | For REST APIs (backend data services) |
Example Return | return "index"; // returns view name | return user; // returns |
✅ 1. @SpringBootApplication
-
Purpose: Entry point of any Spring Boot application.
-
Includes:
@Configuration
,@EnableAutoConfiguration
, and@ComponentScan
. -
Used On: Main class.
✅ 2. @RestController
-
Purpose: Marks the class as a REST API controller.
-
Returns: JSON or XML directly in response.
✅ 3. @Autowired
-
Purpose: Automatically injects (wires) dependencies.
-
Used For: Services, repositories, components.
✅ 4. @GetMapping
/ @PostMapping
-
Purpose: Maps HTTP GET/POST requests to methods.
-
Used In: Controller classes.
✅ 5. @Entity
-
Purpose: Declares a class as a JPA entity for database mapping.
-
Used In: Model classes.
What is API?
API means an application programming interface that is used to communicate data from application to database server.
API uses HTTP Method to communicate data.
1) HttpGet:- to fetch all records from databases
2) HttpPost:- to submit or post record to server from application
3) HttpPut:- to replace or edit the record to server from application
4) HttpDelete:- to delete record from the server from the application
5) HttpPatch:- to partially update the record
6) Connect:- to connect server we can use connect
7) header:- to send only header content such as content type, size, and token
What type of result will be returned by API?
API will return JSON as a result, JSON means JavaScript Object Notation, It is used to provide a structured approach to output using key: value pair.
Syntax of JSON for Single row:-
{'key':value}
Syntax of JSON for Multiple rows
{
[{key:value},{key:value}]
}
all table columns will be worked as a key and table data will work as a value.
spring boot requirements
setting up and running spring boot applications requires the following:
- java 17+
Create Simple Application using Spring Boot:-
✅ What is JPA?
JPA (Java Persistence API) is a specification (not an implementation) for persisting Java objects to a relational database.
-
It defines interfaces and annotations.
-
It is part of Jakarta EE (previously Java EE).
-
It requires an implementation like Hibernate, EclipseLink, or TopLink.
🧠 Think of JPA as a contract.
✅ What is Hibernate?
Hibernate is the most popular implementation of JPA.
-
It is a framework that actually performs the ORM (Object-Relational Mapping).
-
It also provides extra features beyond JPA (like caching, better lazy loading).
🧠 Think of Hibernate as the worker following the JPA contract.
✅ Example: JPA Annotations (Used by Hibernate)
This works in both JPA and Hibernate because Hibernate implements JPA.
✅ Key Differences: JPA vs Hibernate
Feature | JPA (Java Persistence API) | Hibernate |
---|---|---|
Type | Specification | Implementation of JPA |
Provided by | Java (Jakarta) EE | Red Hat (open-source framework) |
Requires implementation | Yes (like Hibernate, EclipseLink) | No, it is the implementation |
Extra features | No (only standard) | Yes (caching, validation, interceptors) |
Vendor lock-in | No | Yes, if you use Hibernate-only features |
Ease of switching frameworks | Easy | Hard (if tightly coupled to Hibernate) |
✅ Which is Better to Use?
Use JPA (with Hibernate):
✔ When you want standard and portable code.
✔ When working in enterprise applications with possible switching of ORM providers.
✔ When using Spring Boot (spring-boot-starter-data-jpa
uses Hibernate under the hood by default).
Use Hibernate directly:
✔ When you want advanced features (2nd level caching, filters, native SQL mapping).
✔ When you are okay with Hibernate-specific code.
✅ In Spring Boot?
Always use:
✅ It uses JPA annotations + Hibernate as the default JPA provider.
JPA Query Syntax:
🔶 1. JPQL (Java Persistence Query Language)
JPQL is object-oriented — you write queries using entity class names and fields, not table/column names.
✅ Example 1: Select all students
✅ Example 2: Select student by name
✅ Example 3: Update student name
🔶 2. Native SQL Query
Native queries use real SQL, directly on your database tables.
✅ Example: Use actual table and column names
🔶 3. Derived Query Methods (No @Query needed)
Spring Data JPA can derive queries based on method names.
✅ Example 1: Find by name
✅ Example 2: Find by name and age
🔶 4. Pagination and Sorting
🔶 5. Delete and Count
Here’s a complete example of a Spring Boot REST API to perform CRUD operations on a Student
model.
✅ Step-by-Step Project Structure:
✅ 1. pom.xml
Dependencies
✅ 2. Student.java
(Model)
✅ 3. StudentRepository.java
✅ 4. StudentService.java
✅ 5. StudentServiceImpl.java
✅ 6. StudentController.java
✅ 7. application.properties
✅ 8. StudentApiApplication.java
✅ Run and Test
-
Run the application.
-
Test endpoints using Postman or curl.
Comments
Post a Comment
POST Answer of Questions and ASK to Doubt