Skip to content

Lab02 http response dto

Create a response object to hold information to be returned.

A. Create DTO

  1. Create a new class, CoffeeOrderResponse

  2. Add 2 member variables:

    1. A String named id
    2. A String named price
  3. Use IntelliJ to generate the getters and setters (CMD + N)

B. Return DTO

  1. Open up the CoffeeOrderApiController and change the coffeeOrderInfo method to return a CoffeeOrderResponse instead of the existing String

  2. Inside the method, create a coffee order:

    1. Create a new CoffeeOrder
    2. Set the size to SMALL
  3. Create a response from the order:

    1. Instantiate a CoffeeOrderResponse
    2. Set the response's ID to the incoming ID
    3. Set the response's Price to the price from the CoffeeOrder
  4. Return the CoffeeOrderResponse

C. Try It Out

  1. Run the CoffeeVendingMachineApplication from within IntelliJ IDEA.

  2. Make a request against the endpoint by opening the URL:

    http://localhost:8080/api/coffeeorders/123
    
  3. You should get something like:

    {"id": "123", "price": "100"}