[go: up one dir, main page]

0% found this document useful (0 votes)
3 views9 pages

Spring Boot First Project (API Creation)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Spring Boot First Project (API Creation)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Spring Boot

Spring Boot First Project (API Creation)


Steps:

1. Spring Boot project banao.


2. @RestController lagao (ye waiter banata hai).
3. Ek function banao aur uspe @GetMapping ya @PostMapping lagao.
4. Browser ya Postman se us API ko test karo.

Create Main Application Class


@SpringBootApplication

public class ApiDemoApplication {

public static void main(String[] args) {

SpringApplication.run(ApiDemoApplication.class, args);

Create a REST Controller


import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class HelloController {

@GetMapping("/hello")

public String sayHello() {

return "Hello, Spring Boot API is working!";

}
Maven in Java Spring Boot
Maven ek helper tool hai jo tumhari project dependencies manage karta hai aur project
build/run karna easy banata hai.

👉 Build = Code ko machine-readable program me convert karke ready karna.

How to Manage Dependencies in Maven

👉 Tum bas pom.xml me dependency likh do, Maven automatically download karke project me
add kar dega.

Steps to Write Dependency in pom.xml:

1. Open the pom.xml file inside the root of your Maven project.
2. Locate or create the <dependencies> tag.
3. Inside <dependencies>, write each <dependency> with the following fields:
groupId → Defines the group/organization of the dependency.
artifactId → The actual library name.
version → The version of the dependency.
scope (optional) → Defines where the dependency is used (compile, test, runtime, etc.).

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<version>3.1.0</version>

</dependency>

</dependencies>

Maven Lifecycle
Default (Build) Lifecycle

Main lifecycle responsible for building and deploying the project.


Important phases include:
1. validate → Validate if the project is correct.
2. compile → Compile source code.
3. test → Run unit tests.
4. package → Package compiled code (JAR/WAR).
5. verify → Verify package meets quality criteria.
6. install → Install package into local repository.
7. deploy → Deploy package to remote repository.

Note:- Programming me bhi wrapper wahi karta hai → ek normal cheez ko extra features ke
saath use karne layak banata hai.

Maven Spring Boot project in IntelliJ typically follows this standard


structure:

project-name/


├── pom.xml # Maven Project Object Model file: dependencies, plugins, build info

├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/demo/
│ │ │ ├── DemoApplication.java # Main Spring Boot application class
│ │ │ ├── controller/ # REST controllers
│ │ │ ├── service/ # Service layer classes

│ │ │ └── repository/ # Repository/DAO classes


│││
│ │ └── resources/
│ │ ├── application.properties # Spring Boot configuration
│ │ ├── static/ # Static resources: CSS, JS, images

│ │ ├── templates/ # Templates: Thymeleaf, FreeMarker, etc.

│ │ └── schema.sql # DB scripts if needed

││
│ └── test/
│ ├── java/
│ │ └── com/example/demo/ # Unit & integration test classes
│ └── resources/
│ └── test configuration files

└── target/ # Compiled classes, packaged jar/war

IOC (Inversion of Control):

Imagine you are ordering food at a restaurant. You don’t cook it yourself; the restaurant
cooks it and serves it to you. In Spring, IOC means Spring gives your objects (beans) to your
code instead of your code creating them.

ApplicationContext:

Think of it as the kitchen and restaurant combined. It knows all the dishes (beans) and can
serve them whenever needed. You just ask, “Give me this bean,” and it gives you a ready-
made object.

Code

@Component

public class Engine {

public void start() {

System.out.println("Engine started!");

@Component
public class Car {

@Autowired

private Engine engine; // Spring injects Engine automatically

public void drive() {

engine.start();

System.out.println("Car is running");

// ApplicationContext will create Engine and Car objects automatically.

@SpringBootApplication

public class DemoApplication {

public static void main(String[] args) {

ApplicationContext context = SpringApplication.run(DemoApplication.class, args);

Car myCar = context.getBean(Car.class); // Getting Car bean

myCar.drive();

Bean
A Bean is like a ready-made LEGO piece. You don’t build it every time; you just take it and
use it in your project.
In Spring, a Bean is an object that Spring creates and manages. You just ask for it, and
Spring gives it to you.
Example:
@Component // This tells Spring: "Make this a bean"

public class Engine {

public void start() {

System.out.println("Engine started!");

@SpringBootApplication

Definition:
@SpringBootApplication is a convenience annotation that combines three key annotations
to configure a Spring Boot application automatically. It marks the main class of a Spring Boot
project.

What it combines:

1. @Configuration – Marks the class as a source of bean definitions.


2. @EnableAutoConfiguration – Tells Spring Boot to automatically configure beans based
on classpath settings, other beans, and property files.
3. @ComponentScan – Tells Spring to scan the current package and sub-packages for
components, configurations, and services.

Code

@SpringBootApplication

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args); // starts Spring Boot app

Annotations

@Component → Tells Spring: "Hey, make this object for me!"


Like a factory making toys automatically for you.

@Autowired → Tells Spring: "Please give me the toy (object) you made."

You don’t make it yourself; Spring gives it.

@RestController → Makes your class a web controller that can send data (JSON) to the
browser.

Like a waiter delivering dishes (data) to customers (browser).

Example:

@Component

public class Engine {

public void start() {

System.out.println("Engine started");

@RestController

public class CarController {

@Autowired

private Engine engine; // Spring injects Engine here

@GetMapping("/drive")

public String drive() {

engine.start();

return "Car is running!";

}
Annotation Purpose Layer/Usage

@Component Create a Spring-managed bean Generic bean/component

@Autowired Inject bean automatically Dependency Injection (DI)

@RestController Handle REST API requests & Controller layer in web


return JSON apps

+-------------------+ +-------------------+

| @Component | | @Component |

| Engine Bean | | Car Bean |

| |<-------->| (Injected by DI) |

+-------------------+ @Autowired

| Spring IOC creates and manages beans

+-------------------+

| @SpringBootApplication |

| ApplicationContext |

+-------------------+

| Handles HTTP request

+-------------------+
| @RestController |

| CarController |

| (Injected Car) |

+-------------------+

| Returns JSON response

Browser / REST Client

{ "status": "Car is running" }

You might also like