חלקPart 7: Java בעולם האמיתיJava in the real world
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
Spring Boot REST עם בדיקותSpring Boot REST with tests
פרויקט Spring Boot, controller, service ו-repository, JSON אוטומטי, ובדיקות עם MockMvc.
A Spring Boot project, controller, service and repository, automatic JSON, and tests with MockMvc.
הנקודות החשובותKey points
- 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.
- שלוש נקודות קצה: כל הספרים, ספר לפי כותר (או 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: aRequestenters the controller, which decides by method and address, calls the service, which calls the repository. - התשובה היא סטטוס ו-JSON.The answer is a status and JSON.
- מדף, צעד 40: API למדףShelf, step 40: an API for the shelf
מונחיםTerms
- RESTREST: סגנון API מעל HTTP: כל דבר הוא משאב בכתובת (
/books/Dune), והשיטה אומרת מה עושים: GET קורא, POST יוצר, PUT מעדכן, DELETE מוחק.An API style over HTTP: everything is a resource at an address (/books/Dune), and the method says what to do: GET reads, POST creates, PUT updates, DELETE deletes. - Spring BootSpring Boot: framework שמרים שרת אינטרנט ב-Java בכמה שורות. מחלקה עם
@RestControllerומתודות עם@GetMapping("/books")הופכות לנקודות קצה.A framework that starts a Java web server in a few lines. A class with@RestControllerand methods with@GetMapping("/books")become endpoints. - שכבותlayers: controller מדבר HTTP (בקשה ותשובה), service מחזיק את הכללים, ו-repository שומר נתונים. כל שכבה מדברת רק עם זו שמתחתיה.The controller speaks HTTP (request and response), the service holds the rules, and the repository stores data. Each layer talks only to the one below.
- נקודת קצהendpoint: שילוב של שיטה וכתובת שהשרת עונה לו, למשל
GET /books/{title}.A method-and-address pair the server answers, e.g.GET /books/{title}. - הזרקת תלויותdependency injection: מחלקה מקבלת את מה שהיא צריכה בבנאי, במקום ליצור אותו. ב-Spring זה קורה לבד; כאן כותבים
new BookController(new BookService(repo)).A class receives what it needs in its constructor, instead of creating it. In Spring that happens for you; here we writenew BookController(new BookService(repo)). - RESTREST: סגנון API מעל HTTP: כל דבר הוא משאב בכתובת (
/books/Dune), והשיטה אומרת מה עושים: GET קורא, POST יוצר, PUT מעדכן, DELETE מוחק.An API style over HTTP: everything is a resource at an address (/books/Dune), and the method says what to do: GET reads, POST creates, PUT updates, DELETE deletes. - Spring BootSpring Boot: framework שמרים שרת אינטרנט ב-Java בכמה שורות. מחלקה עם
@RestControllerומתודות עם@GetMapping("/books")הופכות לנקודות קצה.A framework that starts a Java web server in a few lines. A class with@RestControllerand methods with@GetMapping("/books")become endpoints. - שכבותlayers: controller מדבר HTTP (בקשה ותשובה), service מחזיק את הכללים, ו-repository שומר נתונים. כל שכבה מדברת רק עם זו שמתחתיה.The controller speaks HTTP (request and response), the service holds the rules, and the repository stores data. Each layer talks only to the one below.
- נקודת קצהendpoint: שילוב של שיטה וכתובת שהשרת עונה לו, למשל
GET /books/{title}.A method-and-address pair the server answers, e.g.GET /books/{title}. - הזרקת תלויותdependency injection: מחלקה מקבלת את מה שהיא צריכה בבנאי, במקום ליצור אותו. ב-Spring זה קורה לבד; כאן כותבים
new BookController(new BookService(repo)).A class receives what it needs in its constructor, instead of creating it. In Spring that happens for you; here we writenew BookController(new BookService(repo)).
Cheatsheet
שרת REST ב-Spring BootA REST server in Spring Boot
SpringApplication.run(ShelfApp.class, args);אותן שכבות, ב-Java רגילהThe same layers, in plain Java
BookController api = new BookController(new BookService(new BookRepository()));
System.out.println(api.handle(new Request("POST", "/books", "Dune;Herbert;1965")));
System.out.println(api.handle(new Request("GET", "/books/Dune", "")));
System.out.println(api.handle(new Request("GET", "/books/Hamlet", "")));
System.out.println(api.handle(new Request("GET", "/books", "")));שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|