Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Spring Boot

Uploaded by

mullaabuzar61
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Spring Boot

Uploaded by

mullaabuzar61
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Spring Boot

Spring boot is framework which is used to build services

Web-Services
1. A service which provides a data of particular application is known as webservice
2. Webservice will not having user interface but they have ability to communicate with other project or
frameworks and share the information
3. Webservice are also web applications
4. But all web application is not webservice
5. To build webservice using spring boot we use REST architecture
6. Any application which has been build using REST architecture is known as RESTful webservice or web
applications
7. Since webservice provides service to the various clients they have to share the data in universal language
or the language which can be understandable client that is web services shares the data in JSON or XML
format

JSON (JavaScript OBJECT Notation)


1. Json objects are the lightweight objects we use json to share the data to the rest api’s of spring boot
2. We can store string, Boolean, number and null data in json
Parsing java class into JSON
class Employee{ {
int eid; “eid”:101,

String name; “name”:”Anis”,

Double salary; “salary”:50000.0,

} }

JSON representational for array and collection


class Car{ {
String brand; “brand”: ”TATA”,

String[] colors; “colors”:[”black”, ”blue”, “White”],

List<String> models; “models”:[“Nexon”, ”Punch”, ”Safari”],

} }
class Car{ {
String brand; “brand”: ”TATA”,

Engine engine; “engine”:{

} “eid”:101,

class Engine{ “cc”:1000,

int eid; },

int cc; }

List or Collection {
class Student{ “name”: ”Anis”,

String name; “Subjects”:[ {

List<Subject> subjects; “sid”:101,

} “name”:”JAVA”,

class Subject{ },{(we can add more)},

int sid; ],

String name; }

@SpringBootApplication
 @SpringBootApplication it is a driver class from which application execution will start
 In spring boot maven project, we can have @SpringBootApplication only once
 @SpringBootApplication will do auto configuration and @ComponentScan internally

Creating Controller in Spring Boot Application


1. To create a controller, we need to create a class and add annotated it with @RestController Annotation
2. @RestController is used to build Rest API’s
3. Rest API’s should be designed or created inside controller using a given mapping annotations
Assignment:
Write the difference between @Controller and @RestController
1. @Controller
 Used in Spring MVC to define a web controller that handles HTTP requests.
 Typically returns a view (HTML page). It works with ViewResolver to map the returned string to a
physical view (like a JSP or Thymeleaf page).
 When you want to build web applications that return views (HTML, JSP, etc.).
2. @RestController
 A combination of @Controller and @ResponseBody. Used for REST APIs to return data directly (usually
in JSON or XML format).
 Directly returns data (e.g., JSON or XML) instead of views. Spring automatically serializes the return
object into the specified format.
 Ideal for building RESTful web services and APIs that return data (JSON/XML) rather than views.
Key Difference:
 @Controller is for returning views (e.g., HTML).
 @RestController is for returning data (e.g., JSON/XML), commonly used for RESTful APIs.
==========================================================================================
Mapping Annotation
1. @GetMapping("/_") ===> fetch
2. @PostMapping("/_") ===> save/insert
3. @PutMapping("/_") ===> update record
4. @PatchMapping("/_") ===> update single state
5. @DeleteMapping("/_") ===> delete
@RequestMapping:
In spring boot, we use @RequestMapping to provide a common URL for all the API’s created inside particular
Controller and in this case @RequestMapping should be used above the class

Note:
1. in controller we can have multiple api’s with same url but mapping annotation should be different
2. in controller we can have multiple api’s with same maping annotations but url should be different
==========================================================================================
reading a data from the request inside rest api using following annotation
1. @RequestParam:
a. @RequestParam is used to read the data from the request which is passed as QueryString or
Query Parameter
2. @PathVariable:
a. Whenever we want to read data from the request which is passed through the url itself then we
make use of @PathVariable annotation
b. If we are passing a data through the url it is recommended to pass data name first and then the
actual data in url
3. @RequestHeader:
a. Whenever we want to read header data of http request, we make use of @RequestHeader
b. In header of request the data will be stored in key-value pair
c. To read that data using @RequestHeader we need to declare variable with the name same as
that of key
4. @CookieValue:
a. @CookieValue Is used to read the cookie which is coming to the request
b. The variable declared with @CookieValue annotation and the cookie key should be same
5. @RequestBody:
a. Whenever we want to create json object into java object present inside spring boot application
we make use of @RequestBody
b. @RequestBody it will be used with class reference variable
==========================================================================================

Spring Data JPA


Spring data jpa is framework which is used to perform crud operation from spring boot application

Creating maven project to implement spring data JPA


Create maven project with following dependencies
1. Web
2. Devtools
3. Spring data jpa
4. Driver software

Spring data jpa provides repository which contains


Some methods which helps to do the crud operation on database table

Steps to implement spring data jpa


1. Add db configuration in properties or yaml file
2. Create entity
3. Create repository for that entity

Creating repositories for entity


1. To create repository for particular entity we need to create interface and extend into jpa repository by
providing entity class name and datatype of primary key
Repository Of particular entity will be containing some non-static methods to do a crud operation on the
database they are
1. save() : which saves record insert a table if it is not exist else it will update existing record and return
the object back
2. findAll() : which returns all the records present in the table in the form of list
3. findById() : which returns an Optional object
Optional<T> :
1. It is a class present inside java.util package
2. We use optional object it deal with null data
3. Optional object provides some non-static methods which will helps us to check the availability of the
data and return it
4. Which contain methods like isPresent(), isEmpty(), get(), orElseThrow() etc,.

MediaType
Rest apis to consume or produce a data in particular format that is either in XML or JSON we make use of
MediaType
If we want to configure media type to consume the data we use consumes attribute in mapping annotation
If we want our api to produce the data in particular data format we use produces attribute in mapping
annotation
if we don’t mention any mediatype in spring boot bydefault it will consider it as JSON
if we want to make mediatype as XML then we need to add Jackson dataformat XML as dependency as a
mediatype convertor and use XML mediatype
https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml

in spring boot application, we need to design apis to take request to produce responses
to redesign the response, we make use of ResponseStructure and ResponseEntity because we need to provide
status code, data, message etc. and give it as a response based on this response client will consume and
display the proper message and data on the browser.

Exception handling in spring boot


Whenever there is an exception occurred in spring boot application the application may crashed and some of
the functionality may not be working and also, we want be able to give proper response to the client
So, we need to handle the exception using exception handling mechanism provided by spring boot
To handle the exception in spring boot we need to create a class and annotated with @RestControllerAdvice
annotation
Then after we need design the methods in this class to handle this exception this method should be annotated
with @ExceptionHandler annotation with particular exception class meta data

You might also like