Lab08 order service http
In this lab we want to talk to the real Kitchen service via HTTP. First we'll use the real service to get status information (via GET) and then we'll send an order to the Kitchen via POST and see it up on the Kitchen Display.
GET Status from Kitchen¶
JavaBean (DTO) for the Response¶
We'll be doing a GET that returns JSON, so you'll create a JavaBean that will hold the response.
The JSON will look something like this:
{
"orderNumber": 1,
"kioskId": 123,
"status": "COMPLETED"
}
- Create a new class named
OrderResponse
that goes into theapi
package:package com.welltestedlearning.mealkiosk.api; public class OrderResponse { private Long orderNumber; // This is the Kitchen's internal ID number private Long kioskId; // This is your Meal Kiosk's MealOrder ID private String status; // Current status of the order }
- Have IntelliJ generate the getters & setters for this class.
Create HttpKitchenService¶
We'll be creating an HTTP implementation of the KitchenService
, where we'll use the RestTemplate
to make the request against the remote service.
-
To make sure the tests continue to pass, open the
MealOrderApiIntegrationTest
class and addto the class (above the class name).@ActiveProfiles("test")
Then open up the
FakeKitchenService
and addto the class (above the class name). 1. Run all the tests 1. Set the "production" profile by opening up the@Profile("test")
application.properties
file and add the following:1. Create a new class, namedspring.profiles.active=prod
HttpKitchenService
, that will implement theKitchenService
interface. Note that it will go into a new package:com.welltestedlearning.mealkiosk.kitchen
.package com.welltestedlearning.mealkiosk.kitchen; import com.welltestedlearning.mealkiosk.domain.KitchenService; import com.welltestedlearning.mealkiosk.domain.MealOrder; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service @Profile("prod") public class HttpKitchenService implements KitchenService { private final RestTemplate restTemplate = new RestTemplate(); private static final String KITCHEN_POST_URL = "http://spiral-burger-kitchen-kiosk.herokuapp.com/api/orders"; private static final String KITCHEN_GET_URL = "http://spiral-burger-kitchen-kiosk.herokuapp.com/api/orders/{orderNumber}"; @Override public Long sendOrder(MealOrder mealOrder) { return 1L; } @Override public String statusFor(Long orderNumber) { // Create a Map to hold the parameter (the order number) // use the RestTemplate to make the request to the URL for the given orderNumber // extract the Status from the response // return the status } }
-
In the
statusFor
method, use thegetForObject()
method onRestTemplate
to pass in the URL, theOrderResponse.class
, and the parameters (theorderNumber
).Weather API Example
This is the example for getting the weather using the Basic Weather API:
String weatherUrl = "https://basic-weather.herokuapp.com/api/weather/{zip}"; Map<String, String> uriVariables = new HashMap<>(); uriVariables.put("zip", "53201"); WeatherResponse response = restTemplate.getForObject(weatherUrl, WeatherResponse.class, uriVariables);
-
Open up the
MealOrderApiController
class and replace the hard-coded1L
in thefindMealOrder
, e.g., replace:withString status = kitchenService.statusFor(1L);
String status = kitchenService.statusFor(foundMealOrder.orderNumber());
Try It Out¶
POST an order to your API, and then do a GET and see what the status
is. Then do the GET a few more times for the same ID to see if the status changed.