Lesson 23: Web APIs using REST (Slides)
Please review the following resources before lecture:
- What is HTTP? (Video)
- Java REST API in 10 minutes (Video)
- 1 Min Java Quick Tip | Using the Builder Pattern (Video)
- Exploring REST APIs with Spring MVC
- Build a REST API with Spring and Java Config
- Using Lombok’s @Builder Annotation
- See Lesson 22 resources.
- Complete the Creating a Library API assignment.
- Do pre-work for lesson 27.
We are continuing to build atop the foundation of our library app. For this assignment, you will help implement the API that will be used by a yet-to-come front-end app.
- You will implement the MediaItemsController to enable the following API:
GET /items- Retrieves a list of media itemsPOST /items- Creates a new media itemGET /items/:id- Retrieves a single media item with the given ID.DELETE /items/:id- Deletes a single media item with the given ID.
- Study the tests in MediaItemsControllerTest to understand what you should accept and return in the API.
- You should not need to make any code changes outside of the
com.codedifferently.lesson23.webpackage.
You can run the server using the usual ./gradlew run --console=plain command from the api/java directory. If you want to test that the server is running correctly, you can use curl like so:
curl http://localhost:3001/items | json_ppThe project also includes an OpenAPI user interface (Swagger) for navigating the API. Just visit http://localhost:3001/swagger-ui.html to access it.
Alternatively, you can also test the API using the tool Postman. I recommend installing this tool to make it easier to test things.
Remember that you can debug the API by visiting the main function in Lesson23.java and clicking Debug main. You'll be able to set breakpoints in your code to see what's happening and fix issues.
This project also includes a fully functioning TypeScript version of the Java project. You can visit api/javascript/api_app to execute it using npm start and view the OpenAPI documentation at http://localhost:3000/api (note that it runs on port 3000).
- gRPC vs REST: Comparing API Styles in Practice (Article): This article explains why the stuff most people call REST isn't actually.
