Skip to content

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

  1. Are all tests passing?
  2. Add a new member variable, String id to the MealOrderResponse class
    • Note that this is a String and not a Long
  3. Have IntelliJ generate the getter and setter for id

B. Annotate and Inject Repository

  1. Add the @Repository annotation to the MealOrderRepository class
  2. Run all the tests: do they pass?

C. Add id to Controller's Response

  1. Open the MealOrderApiController class
  2. 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 ?
  3. In the mealOrder() method, copy the id 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

  1. Start the app by running the MealKioskApplication class
  2. 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"}
    
  3. You should see the following JSON because Burger w/Cheese (6), Large Drink (2), and Regular Fries ($3) = $11.

    {"price": 11, "id": "0"}