Spring Boot
Spring Boot
Configuration :
Create a folder “sprongbootApplication” within that create one more folder
“backendApplication” this is for workspace. got to your downloads there you
already dowmloaded the spring boot application copy and past the
application in sprongbootApplication folder right click open (always open
with java easy ). There you will get sts-4.20.1.RELEASE folder. Delete the zip
file.
Initial point :
@SpringBootApplication --- It scans the compoents within the
package.
It contains 3 annotations
1. @SpringBootConfiguration
2. @EnableAutoConfiguration
3. @ComponentScan
@GetMapping :
For example, when adding a new user to a system, you might create a
POST endpoint that receives user data in the request body, processes it,
and saves it to the database.
R – Read: Reading data involves retrieving existing records from the database or storage
system. We can use repository methods or custom queries to fetch data based on specific
criteria.
For example, to retrieve a user by their ID, you might create a GET endpoint that accepts the
ID as a path variable.
For example, to update a user's information, you might create a PUT endpoint that
receives the updated user data and applies the changes to the corresponding record
in the database.
#heibernate properties
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true will show the message in console
weather the database is created or not.
logging.level.org.springframework=debug it will display the extra
information or details
this.game=game;
}
Setter based: dependencies are set by calling setters on your bean.
Field based: no setter no constructor , dependencies are injucted
using reflection.
Here we use @Autowired annotation
@Autowired
private GamingConsole game;
Spring boot
Steps to develop API
1.create entity class
2.provide the application properties and create entity class.
3.once run the project weather it is connected/ running properly or
not .
4. create a package repository and
This repository should implement the JPArepository interface
5.Create a package service
In this package create a corresponding service interface.
Level 4: create a package service implementation
In this package create a class corresponding serviceimpl, this class
should implement service interface. Foe this class we must provide
@Service annotation.
Level 5: create a package controller.
In this package
post man:
Post: create a collection- >new request-> select body-> select row-> insert the values corresponding to
entity attributes. -> select post method ->write corresponding url to your
Status code:
202 Accepted
404 – not found
create 5 packages
1. Entity: classes (@table, @Entity)
2. Repository:
3. Service
4. serviceImpl: @services
5. Controller: RESTCONTROLLERS
Jpa : packaging the project to deploy in server
Dependencies
Spring Web : Build web, including RESTful, applications using Spring MVC. Uses
Apache Tomcat as the default embedded container.
Lombok :
Spring data JPA :
My sql driver
Spring boot dev tools
Spring security
2.Application Properties
server.port=8095
spring.datasource.url=jdbc:mysql://localhost:3306/
sapnapharma?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
# Hibernate Properties
spring.jpa.hibernate.ddl-auto=update
server.error.include-binding-errors=always
Exception Handling:
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
// ServiceImple class
@Override
public Department getOneDepartment(long id) {
Department existingDept=
deptRepository.findById(id).orElseThrow(()->
new ResourceNotFoundException("Department", "id", "id"));
return existingDept;
example 2:
Swagger –It is an open api
Got to web : https://springdoc.org/ ->getting started
Copy and past the dependency in pom.xml file
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.4.0</version>
</dependency>
*Time Stamp :
created at, modified at: @CreationTimestamp, @UpdateTimestamp
Validations :
Add the annotation -- @Validation
Example:
@Table
@Setter
@Getter
public class Greeting {
@NotNull
@NotBlank
@NotEmpty
private String mgs;
@NotNull
@NotBlank
@NotEmpty
private String from;
@NotNull
@NotBlank
@NotEmpty
private String to;
}
//controller class
@RestController
@RequestMapping("/api/v1/mgs")
@RequiredArgsConstructor
public class MsgController {
@PostMapping("/post")
public ResponseEntity<String> createGreeting(@RequestBody
@Valid Greeting greeting, BindingResult bindingResult)
{
if(bindingResult.hasErrors())
{
return new ResponseEntity<String>(" please check the
details and fields properly felids must not null or
blanck",HttpStatus.BAD_REQUEST) ;
}
Attributes:
ex:
@JsonIgnore
Validations :
application property : server.error.include-binding-errors=always
@NotNull : It doent accept null value but accepts
1.Finder methods:
2.JPA methods:
2.SSO/okta/LDAP
1. knowledge based authentication : Password , pin code, Answer to a secret personal question.
it is to implement and easy to use but not fully safe if somebody steal the user name and password they can act like
you.
2.Possession based authentication: phone/text message, key cards and badges, access token device
we can also achieve spring security by adding: spring starter security dependency. by default, user name is root
spring.security.user.password=Pavithra@home2
Interview question.
1. What is Spring Boot, and how does it differ from the Spring
framework?
2. What is bean in springboot ?
"bean" refers to an object that is managed by the Spring IoC
(Inversion of Control) container. In simpler terms, a bean is an
instance of a class that Spring manages and provides to other
parts of your application when needed. Beans are fundamental
building blocks in Spring applications, and they play a crucial role
in achieving loose coupling, dependency injection, and modular
design.
Teaching :
Enhances understanding
Improves communication skills
Builds confidence
Encourages learning
Fosters empathy
How to download docker?
Website : https://www.docker.com/products/docker-desktop/
Install : open->yes->ok-> restart system
Docker is a platform and a set of tools that allows you to develop, deploy, and run
applications in containers. Containers are lightweight, portable, and self-sufficient
environments that contain everything needed to run an application, including the
code, runtime, libraries, and dependencies.
Command prompt
docker –version or docker –v
Important Annotations:
@Transactional :