Spring Boot Engineering / SPRING MVC & REST API / SPR-006
Transfer API
Explore concept graphLatihan soal
HTTP contract proof dengan Java 21
Jalankan fixture tanpa dependency eksternal:
java generation/fixtures/SPR-006/TransferContractFixture.javaOutput yang harus diprediksi:
post-status=201
location=/transfers/tr-001
get-status=200
missing-status=404
invalid-status=400Scope bukti: fixture memakai JDK HttpServer dan HttpClient untuk benar-benar menguji status HTTP serta Location. Fixture bukan Spring Boot, tidak menguji @RestController, binding JSON, @Valid, atau wiring service Spring. Gunakan @WebMvcTest / MockMvc di proyek Spring Boot sendiri untuk kontrak framework dan negative cases.
Konsep 1 / 3
Map Spring MVC controller methods to the correct path, verb, query parameter and header.
Coba prediksi atau perbaiki
What Spring MVC annotation should receive tr-009 in GET /transfers/tr-009 while keeping a separate ?verbose=false query?
Kalau jawabanmu berbeda
Kesalahan yang mungkin: Using @RequestParam for the ID in /transfers/{id}.
Mengapa tidak bekerja: A path variable appears inside the path; a query parameter appears after the question mark.
Coba perbaiki: Change the signature to @PathVariable String id and reserve @RequestParam for optional query inputs.
Periksa hasil sendiri
Hasil yang diharapkan: Two methods map POST /transfers and GET /transfers/{id}; the path ID is not confused with query/header values.
Kalau berbeda: Inspect the concatenation of class-level and method-level mapping paths.
Konsep 2 / 3
Design a POST endpoint with validated input that returns 201, a resource ID and Location header.
Coba prediksi atau perbaiki
A POST handler returns ResponseEntity.ok(id) after creating a resource. What API contract detail is missing?
Kalau jawabanmu berbeda
Kesalahan yang mungkin: Thinking @Valid guarantees rejection even if the DTO has no field constraints or no validation provider.
Mengapa tidak bekerja: @Valid triggers configured validation; without constraints or provider it cannot enforce the intended input rules.
Coba perbaiki: Add a Jakarta Validation constraint and the appropriate Spring Boot validation starter, then assert 400 with MockMvc.
Periksa hasil sendiri
Hasil yang diharapkan: Valid POST maps to 201 with /transfers/{id} and invalid bean constraints map to 400 when validation is configured.
Kalau berbeda: Verify constraint annotations, validation dependency and any custom exception advice or response mapping.
Konsep 3 / 3
Implement GET 200/404 and keep controller logic limited to HTTP translation.
Coba prediksi atau perbaiki
The service returns Optional.empty() for transfer ID tr-404. What should the controller return in this contract?
Kalau jawabanmu berbeda
Kesalahan yang mungkin: Returning 200 with a null body for a nonexistent transfer.
Mengapa tidak bekerja: The requested resource does not exist; the explicitly chosen API contract requires 404.
Coba perbaiki: Map Optional.empty() to ResponseEntity.notFound().build() and assert it via MockMvc.
Periksa hasil sendiri
Hasil yang diharapkan: The standalone Java fixture prints 201/Location, 200 for found, 404 for missing and 400 for blank input.
Kalau berbeda: Inspect the route id handling and distinguish the JDK reference fixture from a real Spring Boot MVC integration test.
Cek kontrak Spring MVC
Jawab tanpa melihat pembahasan dulu. Kamu bisa memperbaiki jawaban dan mencoba lagi. Kuis ini latihan mandiri dan tidak menyimpan skor atau progres.