Skip to content

Lab 1 - HTTP API


Implement a very simple HTTP API that returns a String in response to a GET request, using Spring's MVC framework.


Note: Keep this reference handy:

A. Create the REST Controller Class

  1. Create a new class named CoffeeOrderApiController

  2. Add the @RestController annotation above the class definition:

    public class CoffeeOrderApiController {

B. Create Method for GET Request

Inside the CoffeeOrderApiController class, create a method that will respond (be "routed" or "mapped") to an API request made to http://localhost:8080/api/coffeeorders/123 by following these steps:

  1. Create an CoffeeOrderInfo method with no parameters:

    public String coffeeOrderInfo(String coffeeOrderId) { 
  2. Add the @GetMapping annotation to the CoffeeOrderInfo method

    GetMapping Example

    This method would be invoked if a GET came in to the path /users/999

    public String showUser(@PathVariable("id") String userId) {...}
  3. Inside the coffeeOrderInfo method, return a String, e.g., "your order" and append the ID that came in as a parameter (coffeeOrderId).

C. Try it Out

  1. Run the CoffeeVendingMachineApplication from within IntelliJ IDEA by doing this:

  2. Right click on the CoffeeVendingMachineApplication file and then select "Run..."

  3. Once you see something like the following, you can go to the next step:

    2017-08-27 09:37:21.351  INFO 11472 --- [  Main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2017-08-27 09:37:21.355  INFO 11472 --- [  Main] com.welltestedlearning.cvm.CoffeeVendingMachineApplication  : Started CoffeeVendingMachineApplication in 3.433 seconds (JVM running for 4.064)
  4. Make a request against the endpoint by either:

    1. Browser: opening the URL http://localhost:8080/api/coffeeorders/123

    2. curl: use curl to make the request, e.g.:

      curl --noproxy localhost -v localhost:8080/api/coffeeorders/123

    3. Note: the --noproxy localhost tells curl to bypass the proxy for localhost

  5. You should see something like

    Your order #123