Skip to content

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"
}
We'll create a JavaBean that matches those properties.

  1. Create a new class named OrderResponse that goes into the api 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
    }
    
  2. 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.

  1. To make sure the tests continue to pass, open the MealOrderApiIntegrationTest class and add

    @ActiveProfiles("test")
    
    to the class (above the class name).

    Then open up the FakeKitchenService and add

    @Profile("test")
    
    to the class (above the class name). 1. Run all the tests 1. Set the "production" profile by opening up the application.properties file and add the following:
    spring.profiles.active=prod
    
    1. Create a new class, named HttpKitchenService, that will implement the KitchenService 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
      }
    }
    

  2. In the statusFor method, use the getForObject() method on RestTemplate to pass in the URL, the OrderResponse.class, and the parameters (the orderNumber).

    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);
    
  3. Open up the MealOrderApiController class and replace the hard-coded 1L in the findMealOrder, e.g., replace:

    String status = kitchenService.statusFor(1L);
    
    with
    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.