SpringBoot
SpringBoot
Q) What is Spring?
We can make enterprise level application and
web application using Spring framework.
.war
.jar
Use Cases:
Libraries or frameworks (e.g., Apache
Commons, Hibernate).
Standalone Java applications.
Reusable components for other Java projects.
APPLICATION CONTEXT
Spring.xml
In Spring Framework, you traditionally create an
XML file (like Spring.xml) to configure beans and
define dependencies. However, in Spring Boot,
this is not required in most cases. Spring Boot
was designed to simplify and modernize Spring
applications by eliminating the need for
explicit XML configuration. Instead, it uses
Java-based configuration and convention-
over-configuration principles.
SPRINGBOOT
Springboot isn’t a replacement for Spring
framework, Springboot gives you dependences and
helps you configure JAR files. Springboot helps us
get a production ready project, all the basic
configurations will be done by Springboot.
class Laptop
{
@Autowired
Harddrive obj1;
}
It is used to:
1) Book.java
Code:
package com.example.demo.model;
CODE:
package com.example.demo.repository;
import com.example.demo.model.Book;
import
org.springframework.data.jpa.repository.JpaRepo
sitory;
import
org.springframework.stereotype.Repository;
import java.util.List;
3) BookController.java
CODE:
package com.example.demo.controller;
import
com.example.demo.exception.BookIdMismatchE
xception;
import
com.example.demo.exception.BookNotFoundExc
eption;
import com.example.demo.model.Book;
import
com.example.demo.repository.BookRepository;
import
org.springframework.web.bind.annotation.*;
import java.util.List;
// Delete a book by ID
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
bookRepository.findById(id)
.orElseThrow(BookNotFoundException::
new); // Ensures the book exists
bookRepository.deleteById(id); // Deletes
the book
}
}
Endpoints:
src/WebApp/index.jsp
ii) Create a controller
src/main/java/com.example.demo/
HomeController.java
HttpSession session=req.getSession()
- Retrieves or creates an HttpSession object for
the current user
- If a session already exists for the user it
retrieves it.
- If no session exists it creates a new one.
What is a session?
YES
@RequestParam("name"):
Extracts a query parameter name from the URL.
For example, if the user visits:
http://localhost:8080/Home?name=justin, the
value of name will be "justin".
ModelAndView is a Spring MVC object used to
send both data and a view name to the frontend.
@PostMapping("/alien")
public Alien addAlien(@RequestBody Alien
alien)
{
repo.save(alien);
return alien;
}
DELETE:
@RepositoryRestResource(collectionReso
urceRel="aliens", path="aliens"):
1. This tells Spring Boot to
automatically create REST APIs for
AlienRepo.
2. Without writing a controller, Spring
Boot will:
3. Expose a REST API at
http://localhost:8080/aliens
4. Return JSON data for Alien objects.
5. The parameters:
6. collectionResourceRel="aliens" →
Sets the JSON key name for a
collection of aliens.
7. path="aliens" → Defines the REST
endpoint (/aliens).
What This Code Does Overall
alien.