Lab05 autowire
In this lab, you'll enhance the MealOrderResponse
to include the id
and autowire the repository to the controller using the Spring annotations.
A. Add id Property to MealOrderResponse¶
- Are all tests passing?
- Add a new member variable,
String id
to theMealOrderResponse
class- Note that this is a
String
and not aLong
- Note that this is a
- Have IntelliJ generate the getter and setter for
id
B. Annotate and Inject Repository¶
- Add the
@Repository
annotation to theMealOrderRepository
class - Run all the tests: do they pass?
C. Add id to Controller's Response¶
- Open the
MealOrderApiController
class - If you have a default constructor (no-argument), then delete it.
- ? Why do you need to do this ?
- ? What if you don't delete this ?
- In the
mealOrder()
method, copy theid
from the saved meal order to the MealOrderResponse, e.g.:
MealOrderResponse mealOrderResponse = new MealOrderResponse();
mealOrderResponse.setPrice(price);
mealOrderResponse.setId("" + savedMealOrder.getId()); // convert ID to a String
D. Try it Out¶
- Start the app by running the
MealKioskApplication
class -
Use this URL in either curl or Postman:
localhost:8080/api/mealorder
curl on Windows¶
curl -v -d "{\"burger\": \"cheese\", \"drinkSize\": \"large\", \"friesSize\": \"regular\"}" -H "Content-Type: application/json" localhost:8080/api/mealorder
curl on Mac¶
curl -v -d '{"burger": "cheese", "drinkSize": "large", "friesSize": "regular"}' -H 'Content-Type: application/json' localhost:8080/api/mealorder
Postman¶
Do a POST to
localhost:8080/api/mealorder
with the following JSON{"burger": "cheese", "drinkSize": "large", "friesSize": "regular"}
-
You should see the following JSON because Burger w/Cheese (6), Large Drink (2), and Regular Fries ($3) = $11.
{"price": 11, "id": "0"}