נבנה על ידיBuilt by Ariel Yohimovich
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.
פרויקט Spring Boot, controller, service ו-repository, JSON אוטומטי, ובדיקות עם MockMvc. בסוף המפגש מוסיפים צעד לפרויקט המלווה.A Spring Boot project, controller, service and repository, automatic JSON, and tests with MockMvc. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- שרת REST ב-Spring BootA REST server in Spring Boot
- אותן שכבות, ב-Java רגילהThe same layers, in plain Java
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגש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.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושא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.
שלוש נקודות קצה: כל הספרים, ספר לפי כותר (או 404), והוספה (201). Spring ממיר את ה-record ל-JSON לבד.Three endpoints: every book, a book by title (or 404), and adding (201). 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.
כדי לראות מה Spring עושה בשבילכם: Request נכנס ל-controller, שמחליט לפי שיטה וכתובת, קורא ל-service, שקורא ל-repository. התשובה היא סטטוס ו-JSON. הכול כאן רץ באמת.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. 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
הרחיבו את השכבות: POST עם שנה שלילית מחזיר 400 (ה-service זורק, ה-controller ממיר ל-400), ושיטה לא מוכרת (DELETE) מחזירה 405. הפלט: 201, 200, 404, 400, 201, 200 עם שני ספרים, 405.Extend the layers: a POST with a negative year returns 400 (the service throws, the controller turns it into 400), and an unknown method (DELETE) returns 405. The output: 201, 200, 404, 400, 201, 200 with two books, 405.REST: סגנון API מעל HTTP:REST: An API style over HTTP:
המונחים של המפגשThis session's termsSpring Boot: framework שמרים שרת אינטרנט ב-Java בכמה שורות.Spring Boot: A framework that starts a Java web server in a few lines.
המונחים של המפגשThis session's termsשכבות: controller מדבר HTTP (בקשה ותשובה), service מחזיק את הכללים, ו-repository שומר נתונים.layers: The controller speaks HTTP (request and response), the service holds the rules, and the repository stores data.
המונחים של המפגשThis session's termsנקודת קצה: שילוב של שיטה וכתובת שהשרת עונה לו, למשל GET /books/{title}.endpoint: A method-and-address pair the server answers, e.g.
המונחים של המפגשThis session's termsהזרקת תלויות: מחלקה מקבלת את מה שהיא צריכה בבנאי, במקום ליצור אותו.dependency injection: A class receives what it needs in its constructor, instead of creating it.
המונחים של המפגשThis session's termsשלוש נקודות קצה: כל הספרים, ספר לפי כותר (או 404), והוספה (201).Three endpoints: every book, a book by title (or 404), and adding (201).
שרת REST ב-Spring BootA REST server in Spring BootSpring ממיר את ה-record ל-JSON לבד.Spring turns the record into JSON for you.
שרת REST ב-Spring BootA REST server in Spring Bootכדי לראות מה 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.
אותן שכבות, ב-Java רגילהThe same layers, in plain Javaהתשובה היא סטטוס ו-JSON.The answer is a status and JSON.
אותן שכבות, ב-Java רגילהThe same layers, in plain Javaהכול כאן רץ באמת.Everything here really runs.
אותן שכבות, ב-Java רגילהThe same layers, in plain Javaמדף, צעד 40: API למדףShelf, step 40: an API for the shelf
הצעד של היום בפרויקט מדףToday's step in the Shelf projectאיזו שיטה יוצרת משאב חדש?Which method creates a new resource?
- POSTPOST
- GETGET
- DELETEDELETE
- HEADHEAD
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.POSTPOST
נכון, ובדרך כלל עונה 201.Right, and usually answers 201.בקשה עם שנה 19x5. מה הסטטוס המתאים?A request with the year 19x5. Which status fits?
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.400400
נכון: הלקוח שלח קלט שגוי.Right: the client sent bad input.איפה הכי נכון לבדוק ש"השנה לא שלילית"?Where does "the year isn't negative" belong best?
- ב-serviceIn the service
- ב-repositoryIn the repository
- ב-HTMLIn the HTML
- בשום מקוםNowhere
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.ב-serviceIn the service
נכון: זה כלל עסקי, לא עניין של HTTP או אחסון.Right: it's a business rule, not an HTTP or storage matter.ראוטר קטןA tiny router
route(Request) עם switch על method + " " + path:route(Request) with a switch on method + " " + path:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.משפחות של סטטוסStatus families
status(code) לפי הספרה הראשונה:status(code) by the first digit:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.שלוש שכבותThree layers
- כתבו את
BookRepository, BookService ו-BookController מהתזכורת.Write BookRepository, BookService and BookController from the refresher.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.400 ולא חריגה400, not an exception
add(body) מקבלת title;author;year.add(body) takes title;author;year.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 40: API למדףShelf, step 40: an API for the shelf
- הרחיבו את השכבות:Extend the layers:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.בדיקה עם repository מזויףA test with a fake repository
FakeRepository יורשת מ-BookRepository וסופרת שמירות.FakeRepository extends BookRepository and counts saves.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.דפדוףPaging
page(titles, page, size) מחזירה JSON עם page, items (רק הדף המבוקש) ו-total.page(titles, page, size) returns JSON with page, items (only the requested page) and total.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.ב-Spring זה @GetMapping("/hello").In Spring that's @GetMapping("/hello").
ראוטר קטןA tiny routercode / 100code / 100
משפחות של סטטוסStatus familiesה-controller לא יודע איך שומרים; ה-repository לא יודע מה זה HTTP.The controller doesn't know how things are stored; the repository doesn't know what HTTP is.
שלוש שכבותThree layersשרת לא קורס בגלל קלט רע: הוא עונה 400 עם הסבר.A server doesn't crash on bad input: it answers 400 with an explanation.
400 ולא חריגה400, not an exceptionהחריגה לא יוצאת מה-controller: תופסים וממירים לתשובה.The exception doesn't leave the controller: catch it and turn it into a response.
מדף, צעד 40: API למדףShelf, step 40: an API for the shelfזה מה ש-@MockBean עושה בבדיקות Spring.That's what @MockBean does in Spring tests.
בדיקה עם repository מזויףA test with a fake repositoryמה למדנו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
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.