נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
JavaJava

Spring Boot REST עם בדיקותSpring Boot REST with tests

מפגש 40: פרויקט Spring Boot, controller, service ו-repository, JSON אוטומטי, ובדיקות עם MockMvc.Session 40: A Spring Boot project, controller, service and repository, automatic JSON, and tests with MockMvc.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • שרת REST ב-Spring BootA REST server in Spring Boot
  • אותן שכבות, ב-Java רגילהThe same layers, in plain Java
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • REST: סגנון API מעל HTTP:REST: An API style over HTTP:
  • Spring Boot: framework שמרים שרת אינטרנט ב-Java בכמה שורות.Spring Boot: A framework that starts a Java web server in a few lines.
  • שכבות: controller מדבר HTTP (בקשה ותשובה), service מחזיק את הכללים, ו-repository שומר נתונים.layers: The controller speaks HTTP (request and response), the service holds the rules, and the repository stores data.
  • נקודת קצה: שילוב של שיטה וכתובת שהשרת עונה לו, למשל GET /books/{title}.endpoint: A method-and-address pair the server answers, e.g.
  • הזרקת תלויות: מחלקה מקבלת את מה שהיא צריכה בבנאי, במקום ליצור אותו.dependency injection: A class receives what it needs in its constructor, instead of creating it.
נושאTopic 2

שרת REST ב-Spring BootA REST server in Spring Boot

שרת REST ב-Spring BootA REST server in Spring Boot

  • שלוש נקודות קצה: כל הספרים, ספר לפי כותר (או 404), והוספה (201).Three endpoints: every book, a book by title (or 404), and adding (201).
  • Spring ממיר את ה-record ל-JSON לבד.Spring turns the record into JSON for you.
נושאTopic 3

אותן שכבות, ב-Java רגילהThe same layers, in plain Java

אותן שכבות, ב-Java רגילהThe same layers, in plain Java

  • כדי לראות מה Spring עושה בשבילכם: Request נכנס ל-controller, שמחליט לפי שיטה וכתובת, קורא ל-service, שקורא ל-repository.To see what Spring does for you: a Request enters the controller, which decides by method and address, calls the service, which calls the repository.
  • התשובה היא סטטוס ו-JSON.The answer is a status and JSON.
  • הכול כאן רץ באמת.Everything here really runs.
נושאTopic 4

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 40: API למדףShelf, step 40: an API for the shelf

REST: סגנון API מעל HTTP:REST: An API style over HTTP:

Spring Boot: framework שמרים שרת אינטרנט ב-Java בכמה שורות.Spring Boot: A framework that starts a Java web server in a few lines.

שכבות: controller מדבר HTTP (בקשה ותשובה), service מחזיק את הכללים, ו-repository שומר נתונים.layers: The controller speaks HTTP (request and response), the service holds the rules, and the repository stores data.

נקודת קצה: שילוב של שיטה וכתובת שהשרת עונה לו, למשל GET /books/{title}.endpoint: A method-and-address pair the server answers, e.g.

הזרקת תלויות: מחלקה מקבלת את מה שהיא צריכה בבנאי, במקום ליצור אותו.dependency injection: A class receives what it needs in its constructor, instead of creating it.

שלוש נקודות קצה: כל הספרים, ספר לפי כותר (או 404), והוספה (201).Three endpoints: every book, a book by title (or 404), and adding (201).

Spring ממיר את ה-record ל-JSON לבד.Spring turns the record into JSON for you.

כדי לראות מה Spring עושה בשבילכם: Request נכנס ל-controller, שמחליט לפי שיטה וכתובת, קורא ל-service, שקורא ל-repository.To see what Spring does for you: a Request enters the controller, which decides by method and address, calls the service, which calls the repository.

התשובה היא סטטוס ו-JSON.The answer is a status and JSON.

הכול כאן רץ באמת.Everything here really runs.

מדף, צעד 40: API למדףShelf, step 40: an API for the shelf

איזו שיטה יוצרת משאב חדש?Which method creates a new resource?

  • POSTPOST
  • GETGET
  • DELETEDELETE
  • HEADHEAD

POSTPOST

בקשה עם שנה 19x5. מה הסטטוס המתאים?A request with the year 19x5. Which status fits?

  • 400400
  • 500500
  • 200200
  • 404404

400400

איפה הכי נכון לבדוק ש"השנה לא שלילית"?Where does "the year isn't negative" belong best?

  • ב-serviceIn the service
  • ב-repositoryIn the repository
  • ב-HTMLIn the HTML
  • בשום מקוםNowhere

ב-serviceIn the service

ראוטר קטןA tiny router

  • route(Request) עם switch על method + " " + path:route(Request) with a switch on method + " " + path:

משפחות של סטטוסStatus families

  • status(code) לפי הספרה הראשונה:status(code) by the first digit:

שלוש שכבותThree layers

  • כתבו את BookRepository, BookService ו-BookController מהתזכורת.Write BookRepository, BookService and BookController from the refresher.

400 ולא חריגה400, not an exception

  • add(body) מקבלת title;author;year.add(body) takes title;author;year.

מדף, צעד 40: API למדףShelf, step 40: an API for the shelf

  • הרחיבו את השכבות:Extend the layers:

בדיקה עם repository מזויףA test with a fake repository

  • FakeRepository יורשת מ-BookRepository וסופרת שמירות.FakeRepository extends BookRepository and counts saves.

דפדוףPaging

  • page(titles, page, size) מחזירה JSON עם page, items (רק הדף המבוקש) ו-total.page(titles, page, size) returns JSON with page, items (only the requested page) and total.

ב-Spring זה @GetMapping("/hello").In Spring that's @GetMapping("/hello").

code / 100code / 100

ה-controller לא יודע איך שומרים; ה-repository לא יודע מה זה HTTP.The controller doesn't know how things are stored; the repository doesn't know what HTTP is.

שרת לא קורס בגלל קלט רע: הוא עונה 400 עם הסבר.A server doesn't crash on bad input: it answers 400 with an explanation.

החריגה לא יוצאת מה-controller: תופסים וממירים לתשובה.The exception doesn't leave the controller: catch it and turn it into a response.

זה מה ש-@MockBean עושה בבדיקות Spring.That's what @MockBean does in Spring tests.

מה למדנוWhat we learned

  • REST: סגנון API מעל HTTP:REST: An API style over HTTP:
  • שלוש נקודות קצה: כל הספרים, ספר לפי כותר (או 404), והוספה (201).Three endpoints: every book, a book by title (or 404), and adding (201).
  • כדי לראות מה Spring עושה בשבילכם: Request נכנס ל-controller, שמחליט לפי שיטה וכתובת, קורא ל-service, שקורא ל-repository.To see what Spring does for you: a Request enters the controller, which decides by method and address, calls the service, which calls the repository.
  • מדף, צעד 40: API למדףShelf, step 40: an API for the shelf

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich