0% found this document useful (0 votes)
8 views19 pages

Unit 5 Spring Boot

The document provides an overview of Spring Boot build systems, highlighting Maven and Gradle as popular tools for managing dependencies in Java applications. It discusses the structure of Spring Boot applications, the use of Application and Command Line Runner interfaces, and the implementation of web services, particularly focusing on RESTful and SOAP services. Additionally, it outlines the project structure for building RESTful web services, including model and controller classes for managing student data.

Uploaded by

roopranjan
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)
8 views19 pages

Unit 5 Spring Boot

The document provides an overview of Spring Boot build systems, highlighting Maven and Gradle as popular tools for managing dependencies in Java applications. It discusses the structure of Spring Boot applications, the use of Application and Command Line Runner interfaces, and the implementation of web services, particularly focusing on RESTful and SOAP services. Additionally, it outlines the project structure for building RESTful web services, including model and controller classes for managing student data.

Uploaded by

roopranjan
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

Spring Boot

Spring Boot - Build Systems


• Maven and Gradle are the two most popular build tools to build java based
applications.
• It is strongly recommended that you choose a build system that
supports dependency management.
• Maven and Gradle are the two most popular build tools to build java based
applications. Both support dependency management and consume antifactory
from maven central.
Dependency Management
• Spring Boot team provides a list of dependencies to support the Spring Boot
version for its every release.
• You do not need to provide a version for dependencies in the build configuration
file.
• Spring Boot automatically configures the dependencies version based on the
release.
Spring Boot - Build Systems
Gradle
• is a more modern build tool that uses a Groovy or Kotlin DSL ([Link] or
[Link]) for configuration.
• It is known for its performance due to features like incremental builds and build
caching.
• Gradle is also highly customizable and often preferred in larger or more complex
projects.

• Spring Boot provides dedicated plugins for both Maven (spring-boot-maven-


plugin) and Gradle ([Link]), which simplify tasks like
running the application, managing dependencies, and creating executable JAR
files.
• The choice between Maven and Gradle depends on team preference, familiarity,
and project requirements.
Spring Boot - Build Systems
Maven Dependency
• For Maven configuration, we should inherit the Spring Boot Starter parent project
to manage the Spring Boot Starters dependencies.
• For this, simply we can inherit the starter parent in our [Link] file as shown
below.

<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
</parent>
Spring Boot - Build Systems
Gradle Dependency
• We can import the Spring Boot Starters dependencies directly
into [Link] file.
Observe the code given below −
Spring Boot - Build Systems
plugins block: Specifies the plugins used in the project.
•[Link]: Adds Spring Boot support, including tasks like bootRun,
bootJar, and auto-configuration.
•java: Applies the standard Java plugin to compile Java code.

•group: Defines the base package or organization name. It's used for publishing or
structuring artifacts.
•version: Sets the version of your application.
•sourceCompatibility: Sets the Java version (e.g., Java 17) to compile the source
code.
Spring Boot - Build Systems
repositories block: Declares where Gradle should look for dependencies.
•mavenCentral(): A popular public repository that hosts open-source libraries,
including Spring Boot artifacts.
Spring Boot - Code Structure
The typical code structure of a Spring Boot application follows standard
Maven/Gradle conventions and the principles of layered architecture.
Spring Boot -Runners
Application Runner and Command Line Runner interfaces lets you to execute
the code after the Spring Boot application is started.
You can use these interfaces to perform any actions immediately after the
application has started.
Application Runner
• Application Runner is an interface used to execute the code after the Spring Boot
application started. The example given below shows how to implement the
Application Runner interface on the main class file.
Spring Boot -Runners
Application Runner
Spring Boot -Runners
• Command Line Runner interfaces lets you to execute the code after the Spring
Boot application is started.
• Command Line Runner is an interface. It is used to execute the code after the
Spring Boot application started.
Spring Boot – Web Service
• A web service is a software system designed to support interoperable
machine-to-machine communication over a network (typically the internet).
• A web service is a set of open protocols and standards that allow data to be
exchanged between different applications or systems.
• Web services can be used by software programs written in a variety of
programming languages and running on a variety of platforms to exchange data
via computer networks such as the Internet in a similar way to inter-process
communication on a single computer.

Types of Web Services


There are mainly two types of web services:
• SOAP web services
• RESTful web services
Spring Boot – Web Service
SOAP
• SOAP (Simple Object Access Protocol) is a protocol for exchanging
structured information in the implementation of web services.
• Uses only XML for messaging
• Operates over protocols like HTTP, SMTP, FTP
• Highly standardized with WSDL (Web Services Description Language)

REST (Representational State Transfer) is an architectural style that uses


standard HTTP methods to expose resources.
• Supports multiple formats: JSON, XML, plain text, HTML
• Stateless, simple, and fast
• Commonly used in modern web and mobile applications
Spring Boot – Web Service
SOAP
• SOAP (Simple Object Access Protocol) is a protocol for exchanging
structured information in the implementation of web services.
• Uses only XML for messaging
• Operates over protocols like HTTP, SMTP, FTP
• Highly standardized with WSDL (Web Services Description Language)

REST (Representational State Transfer) is an architectural style that uses


standard HTTP methods to expose resources.
• Supports multiple formats: JSON, XML, plain text, HTML
• Stateless, simple, and fast
• Commonly used in modern web and mobile applications
Spring Boot – Web Service
REST Services
• In REST architecture, a REST Server simply provides access to resources and
REST client accesses and modifies the resources.
• Here each resource is identified by URIs/ global IDs.
• REST uses various representation to represent a resource like text, JSON, XML.
JSON is the most popular one.

HTTP methods
• Following four HTTP methods are commonly used in REST based architecture.
• GET − Provides a read only access to a resource. To retrieve data from the
server.
• POST − Used to create a new resource. e.g. add new students record
• DELETE − Used to remove a resource. e.g. delete students record
• PUT − Used to update a existing resource or create a new resource.
Spring Boot – Web Service
REST Services

HTTP Method Action Used For

GET Read Fetch data

POST Create Add new data

PUT Update Modify existing data

DELETE Delete Remove existing data


Spring Boot – BUILDING RESTFUL WEB SERVICES
Project Structure
Spring Boot – BUILDING RESTFUL WEB SERVICES
Model Class:
• This is a model that represents the structure of a Student object.
•Contains private fields: id, name, course
•Has constructors, getters, and setters to access or modify these fields
Spring Boot – BUILDING RESTFUL WEB SERVICES
Controller Class:
A REST controller that handles HTTP requests (GET, POST, PUT, DELETE) for
managing students in-memory.
•Maps requests to /api/students using @RequestMapping.
•Uses @GetMapping, @PostMapping, @PutMapping, @DeleteMapping for various
HTTP operations.
Contains Methods
Method HTTP Verb Description
getAllStudents() GET Returns all students
getStudentById(int id) GET Returns student by ID
addStudent(Student student) POST Adds a new student
updateStudent(int id,
PUT Updates an existing student
Student student)
deleteStudent(int id) DELETE Deletes a student by ID

You might also like