0% found this document useful (0 votes)
26 views12 pages

Spring Boot 0

This document summarizes key aspects of building a REST API with Spring Boot including: 1) Using JPA and Hibernate for data access and CRUD operations on entities like User and Book. 2) Implementing controllers with @GetMapping, @PostMapping etc. to handle HTTP requests and returning JSON responses. 3) Connecting to a database using JPA and implementing DAO and service layers for data access. 4) Using Thymeleaf for templating and including fragments to reuse content across pages. 5) Enabling inheritance of layout and common elements across pages using Thymeleaf.

Uploaded by

Prasad Jagtap
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)
26 views12 pages

Spring Boot 0

This document summarizes key aspects of building a REST API with Spring Boot including: 1) Using JPA and Hibernate for data access and CRUD operations on entities like User and Book. 2) Implementing controllers with @GetMapping, @PostMapping etc. to handle HTTP requests and returning JSON responses. 3) Connecting to a database using JPA and implementing DAO and service layers for data access. 4) Using Thymeleaf for templating and including fragments to reuse content across pages. 5) Enabling inheritance of layout and common elements across pages using Thymeleaf.

Uploaded by

Prasad Jagtap
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
You are on page 1/ 12

it is sub module of spring

Applcation.peoperties

JPA (java persistance api) (internally use hibernate)

UserRepository User -> gettre,setter,toString

create read

update delete

delete all
custom query UserRepository (interface)

JPQL java persistance query language (line 17 , 18) and native QL


@Param autocreated class

API (Applicartion Programming Interface)

if we give @ResponseBody then


method will not retun view name but normal string on browser
Rest 1) Read -> GET (@GetMapping)

book.java -> getter,setter,toString

BookController

• to return string to browser use @RequestBody


• if we use @RestController then @RequestBody is not required
• also @RequestMapping(value=”/books”, methos= RequestMethod.get)
can be replaced by @GetMapping(“/books”)

same as above using Rest (return JSON)

edited BookController BookService


@ParamVariable

continue
2) CREATE -> POST (@PostMapping)

add to BookService.java add to BookController

• use method post , select body TAb , select raw tab


• write one JSON
• and send post request

3) Delete -> @DeleteMapping

add to BookService.java add to BookController

• use method delete , select body TAb , select raw tab


• on url pass id
• and send post request

4) Update Put -> @PutMapping

add to BookService.java add to BookController

• use method put , select body TAb , select raw tab


• write one JSON
• on url pass id
• and send put request
ResponseEntity | Handling HttpStatus while creating Rest api

• to handle exception we can use ResponseEntity


• which is extension of HttpEntity
• that add HttpStatus code

BookController get (list of books)

• return type is ResponseEntity of List of Books -> which can return


• HttpStatus & data(list)
• if list dones not contain any books then status will be 404 not found
• and if list contains books then (200 ok)

BookController get book BookController post add book

BookController delete book BookController put update book

Connecting with Database with JPA | Rest

application.properties pom.xml

• edit pom.xml with adding all the necessary


dependencies
BookRepository (dao)

------------------- ------------------------- BookService ---------------------------------------------------


getBooks
create BookRepositoty object

getBookById update

deleteBook

addBook

BookController is notChanged just 1 change in getBooks() line 34

use postman for CRUD operation


• POST ,GET,,PUT,DELETE
• use -> localhost:8080/books -> post,get
• use -> localhost:8080/books/id -> delete,update,getbookByid

one to one in (Book)


Book Entity

• change author to Author type line 23 and 24


• cascade all for -> foren key
• and generate getter , setter , toString again
• We HAVE to create --->>> Author Entity

Author Entity Postman

one to one in (Author)


Book Entity Author Entity

• here in above we have given mapping in book and author so we can get author from book and book from author
• but when we getbook -> it will get author (but author has book) so author will getbook again book will getAuthor
• this will be infinite loop
• so to solve this problem @JsonManagedRefernce and @JsonBackReference is used
• @JsonManagedRefernce -> in parent (Book)
• @ JsonBackReference -> in child (Author) so it will manage that infinite loop
Thymleaf

controller

about.html in templates folder


MyController iterate.html

Conditional in thymleaf condition.html

controller

list contail more than one elemennt


beacuse of line 53 in controller
Including Thymeleaf Templates | Include | insert | replace

Replace -> will replace host tag with fragment tag

insert -> insert fragment tag as body in host tag

include -> will only copy content of fragment tag

controller
footer.html (fragment)

service.html

line 11 -> th.insert=”htmlName::fragmentName”

th.replace=”htmlName::fragmentName”
th.include=”htmlName::fragmentName”

thymeleaf passing dynamic value to template while including |


Controller Service.html (host) footer.html (fragment)

• to send dynamic content we have send title , subtitle from controller to service.html ,
• service.html is sending this dynamic content as parameter to the footer.html
• footer.html will be printing all this dynamic content and returned to service.html and service.html will print all the footer.html with all content
Inheriting Thymeleaf Templare | This thing used in project |

controller

Base.html

• line 2 -> other page (about,contact) content will


come here as parameter
-->> th.fragment=”fragmentName(parameter)”
• line 13 -> content will be replaced here
-->> ${parameter} style.css

• line 7 -> has link to css files

about.html

• line 2 -> this page will be replaced by base.html


• content (<section>) this will go to base.html and
pasted between <h1>common header</h1> and
<h1>common footer </h1>
• to send content use
”htmlName::fragmentName(~{::tag})”

about browser

You might also like