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

JavaJava
40
חלק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.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

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

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

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

תרגיליםExercises

תרגילExercise 1

ראוטר קטןA tiny router

route(Request) עם switch על method + " " + path: GET /hello ו-GET /health עונים 200 עם JSON, וכל השאר 404. הדפיסו את שלוש התשובות.

route(Request) with a switch on method + " " + path: GET /hello and GET /health answer 200 with JSON, everything else 404. Print the three responses.

איך הפלט אמור להיראות?What should the output look like?
200 {"message": "hello"}
200 {"status": "up"}
404 {"error": "not found"}
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: ב-Spring זה @GetMapping("/hello").In Spring that's @GetMapping("/hello").
לפתרוןSolution
תרגילExercise 2

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

status(code) לפי הספרה הראשונה: 2xx success, 4xx client error, 5xx server error. הדפיסו עבור 200, 201, 400, 404, 500.

status(code) by the first digit: 2xx success, 4xx client error, 5xx server error. Print for 200, 201, 400, 404, 500.

איך הפלט אמור להיראות?What should the output look like?
200 success
201 success
400 client error
404 client error
500 server error
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: code / 100code / 100
לפתרוןSolution
תרגילExercise 3

שלוש שכבותThree layers

כתבו את BookRepository, BookService ו-BookController מהתזכורת. POST של Dune (201), GET של Dune (200), GET של Hamlet (404), GET של כולם (200 עם מערך).

Write BookRepository, BookService and BookController from the refresher. POST Dune (201), GET Dune (200), GET Hamlet (404), GET all (200 with an array).

איך הפלט אמור להיראות?What should the output look like?
201 {"title": "Dune", "author": "Herbert", "year": 1965}
200 {"title": "Dune", "author": "Herbert", "year": 1965}
404 {"error": "not found"}
200 [{"title": "Dune", "author": "Herbert", "year": 1965}]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: ה-controller לא יודע איך שומרים; ה-repository לא יודע מה זה HTTP.The controller doesn't know how things are stored; the repository doesn't know what HTTP is.
לפתרוןSolution
תרגילExercise 4

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

add(body) מקבלת title;author;year. חסר שדה: 400 {"error": "expected title;author;year"}. שנה לא מספרית: 400 {"error": "year must be a number"}. אחרת 201 עם JSON.

add(body) takes title;author;year. A missing field: 400 {"error": "expected title;author;year"}. A non-numeric year: 400 {"error": "year must be a number"}. Otherwise 201 with JSON.

איך הפלט אמור להיראות?What should the output look like?
201 {"title": "Dune", "author": "Herbert", "year": 1965}
400 {"error": "expected title;author;year"}
400 {"error": "year must be a number"}
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: שרת לא קורס בגלל קלט רע: הוא עונה 400 עם הסבר.A server doesn't crash on bad input: it answers 400 with an explanation.
לפתרוןSolution
תרגילExercise 5

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

פרויקט מלווהCompanion project

הרחיבו את השכבות: 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.

איך הפלט אמור להיראות?What should the output look like?
201 {"title": "Dune", "author": "Herbert", "year": 1965}
200 {"title": "Dune", "author": "Herbert", "year": 1965}
404 {"error": "not found"}
400 {"error": "year must be >= 0"}
201 {"title": "Emma", "author": "Austen", "year": 1815}
200 [{"title": "Dune", "author": "Herbert", "year": 1965}, {"title": "Emma", "author": "Austen", "year": 1815}]
405 {"error": "method not allowed"}
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: החריגה לא יוצאת מה-controller: תופסים וממירים לתשובה.The exception doesn't leave the controller: catch it and turn it into a response.
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

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

FakeRepository יורשת מ-BookRepository וסופרת שמירות. בנו את ה-controller מעליה, שלחו POST תקין ו-POST עם שנה שלילית, והדפיסו true true saves=1 (הבקשה הרעה לא הגיעה לאחסון).

FakeRepository extends BookRepository and counts saves. Build the controller over it, send a good POST and a POST with a negative year, and print true true saves=1 (the bad request never reached storage).

איך הפלט אמור להיראות?What should the output look like?
true true saves=1
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: זה מה ש-@MockBean עושה בבדיקות Spring.That's what @MockBean does in Spring tests.
לפתרוןSolution
אתגרChallenge 2

דפדוףPaging

page(titles, page, size) מחזירה JSON עם page, items (רק הדף המבוקש) ו-total. עבור 5 כותרים ו-size 2: דף 0 ["Dune", "Emma"], דף 2 ["Solaris"], דף 9 [].

page(titles, page, size) returns JSON with page, items (only the requested page) and total. For 5 titles and size 2: page 0 ["Dune", "Emma"], page 2 ["Solaris"], page 9 [].

איך הפלט אמור להיראות?What should the output look like?
{"page": 0, "items": ["Dune", "Emma"], "total": 5}
{"page": 2, "items": ["Solaris"], "total": 5}
{"page": 9, "items": [], "total": 5}
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: Math.min על הגבולות, כדי שדף גבוה מדי יחזיר ריק ולא חריגה.Math.min on the bounds, so a page too far returns empty instead of throwing.
לפתרוןSolution

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