Professional Documents
Culture Documents
CHAPTER -I
(with 4 New Pages in
last)
You can use Spring Boot to create Java applications that can be started by using java
-jar or more traditional war deployments.
Our primary goals are:
● Provide a radically faster and widely accessible getting-started experience for
all Spring development.
● Be opinionated out of the box but get out of the way quickly as requirements
start to diverge from the defaults.
● Provide a range of non-functional features that are common to large classes
of projects (such as embedded servers, security, metrics, health checks, and
externalized configuration).
● Absolutely no code generation (when not targeting native image) and no
requirement for XML configuration.
System Requirements
Spring Boot 3.0.3 requires Java 17 and is compatible up to and including Java 19.
Spring Framework 6.0.5 or above is also required.
Explicit build support is provided for the following build tools:
Build Tool Version
Maven 3.5+
Useful Links :
1.STS installation
https://spring.io/tools
2.MySql installation
https://dev.mysql.com/downloads/mysql/
3.DBeaver Installation
https://dbeaver.io/download/
4.Postman Installation
https://www.postman.com/downloads/
https://www.youtube.com/results?search_query=postman+install+
Ref: https://codippa.com/lombok/installing-lombok
3. Downloading simple demo project
A. Visit : https://start.spring.io/
B. Select minimum required dependency for spring project.
1.Spring Web
2.Lombok
3.Mysql
4.Spring Data Jpa
5.Spring Boot dev tools
#datasource configurations setup (write this configuration in
application.properties)
spring.datasource.url=jdbc:mysql://localhost:3306/spring-boot-demo?useJDBCCompliantTimezone
Shift=true&createDatabaseIfNotExist=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=12345678
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
server.port=8080
What is JSON data?
JSON (JavaScript Object Notation) is the most widely used data format for
data interchange on the web. JSON is a lightweight text-based,
data-interchange format and it is completely language-independent. It is
based on a subset of the JavaScript programming language and it is easy to
understand and generate.
1. String
2. Number
3. Boolean
4. Null
5. Object
6. Array
Example of JSON document:
{
"Geeks" : [
{
"Geekname" : "Sahil kumar",
"subject" : "Data structures",
"Articles" : 10
},
{
"Geekname" : "Pawan singh",
"subject" : "Algorithms",
"Articles" : 16
},
{
"Geekname" : "Ayush Goel",
"subject" : "Networking",
"Articles" : 7
}
]
}
Here I am providing basic data flow structure and processing classes used for any
modules.
@RestController
@SpringBootApplication
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
@ComponentScan(basePackages = { "com" })
@EntityScan(basePackages = {"com.example.demo.entities"})
public class MyDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@RestController
@RequestMapping(value = "/api/v1/demo", produces =
MediaType.APPLICATION_JSON_VALUE)
public class DemoController {
@Autowired
Private DemoService demoService;
@RequestMapping(method = {RequestMethod.POST })
public ResponseEntity save(@RequestBody DemoRequest request)
{
return demoService.save(request);
}
@RequestMapping(method = {RequestMethod.PUT })
public ResponseEntity update(@RequestBody DemoRequest
request) {
return demoService.update(request);
}
@RequestMapping( method = {RequestMethod.DELETE })
public ResponseEntity delete(
@RequestParam(value="id", required=true,default=”0”) long id
) {
return demoService.delete(id);
}
}
@Component
public class DemoService {
@Autowired
private DemoRepository demoRepo;
@Entity
@Table(name=”demo”)
class @Data Demo{
@Id
@Column(name=”demo_id”)
private long id;
@Column(name=”name”)
private String name;
@Column(name=”age”)
private int age;
}
7.Persistence interface creation
We don't need to use @Repository or @Component or @Service annotation when we are
extending JpaRepository because Spring detects that the predefined JpaRepository has
been extended and recognises the interface that extends JpaRepository as a repository.
Although there is not much code here, quite a lot is going on. We step through the
important parts in the next few sections.
$ mvn spring-boot:run
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.3)
....... . . .
....... . . . (log output here)
....... . . .
........ Started MyApplication in 0.906 seconds (process running for 6.514)
Note: If you open a web browser to localhost:8080, you should see the following
output:
Hello World!
To gracefully exit the application, press ctrl-c.
Dependency Management
Each release of Spring Boot provides a curated list of dependencies that it supports.
In practice, you do not need to provide a version for any of these dependencies in
your build configuration, as Spring Boot manages that for you. When you upgrade
Spring Boot itself, these dependencies are upgraded as well in a consistent way.
Each release of Spring Boot is associated with a base version of the Spring
Framework. We highly recommend that you do not specify its version.
Simply put, this allows for loose coupling of components and moves the
responsibility of managing components onto the container.
https://www.baeldung.com/spring-dependency-injection
https://www.baeldung.com/spring-autowire
In this short tutorial, we'll explore the main differences between properties and YAML
files.
https://www.baeldung.com/spring-boot-yaml-vs-properties
https://www.geeksforgeeks.org/difference-between-yaml-yml-and-properties-file-in-java-s
pringboot/
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
https://www.javatpoint.com/maven-pom-xml
https://www.tutorialspoint.com/spring_boot/spring_boot_build_systems.htm
Understanding logging in Spring Boot
https://medium.com/javarevisited/understanding-logging-in-spring-boot-ac0fd79177
b4#:~:text=Spring%20Boot%20comes%20with%20SLF4J,framework%20comes%20
with%20three%20elements
Other References
https://www.geeksforgeeks.org/spring-dependency-injection-with-example/
https://www.javatpoint.com/spring-boot-properties
1.ResponseEntityObject:-
Example:
public ResponseEntity getList(){
try {
List<Demo> demoList =demoRepo.findAll();
}catch(Exception e) {
e.printStackTrace();
2.DemoResponse Model
—----THANK YOU—--