Lab01 http api
Goal¶
Implement a very simple HTTP API that returns a String in response to a GET request, using Spring's MVC framework.
Reference¶
Note: Keep this reference handy: http://engineering.pivotal.io/post/must-know-spring-boot-annotations-controllers/
A. Create the REST Controller Class¶
-
Create a new class named
CoffeeOrderApiController
-
Add the
@RestController
annotation above theclass
definition:@RestController 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:
-
Create an
CoffeeOrderInfo
method with no parameters:public String coffeeOrderInfo(String coffeeOrderId) { }
-
Add the
@GetMapping
annotation to theCoffeeOrderInfo
methodGetMapping Example
This method would be invoked if a GET came in to the path
/users/999
@GetMapping("/users/{id}") public String showUser(@PathVariable("id") String userId) {...}
-
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¶
-
Run the
CoffeeVendingMachineApplication
from within IntelliJ IDEA by doing this: -
Right click on the
CoffeeVendingMachineApplication
file and then select "Run..." -
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)
-
Make a request against the endpoint by either:
-
Browser: opening the URL
http://localhost:8080/api/coffeeorders/123
-
curl
: usecurl
to make the request, e.g.:curl --noproxy localhost -v localhost:8080/api/coffeeorders/123
-
Note: the
--noproxy localhost
tellscurl
to bypass the proxy forlocalhost
-
-
You should see something like
Your order #123