Activity: Creating a List of Blog Articles as a REST Resource

Prerequisites

We are building this activity upon the blogmania application that we built in the previous chapters. It already has a human-facing web frontend. You can find the source code at https://bit.ly/2OxaeoF. It is the same as we left it in the previous activity that we've covered, so you can reuse that project or create a new one as you wish.

Aim

To create a list of current blog articles that will be available as a REST resource to query with Postman.

Scenario

You have a working application for humans and are asked to add another interface to it, suited for other programs as clients. You decide to do it the RESTful way.

Steps for Completion

  1. Take the blogmania application and find the BlogPostController.
  2. Write a REST controller that gives a list of all the articles under the URL /api/blogposts.
  3. Add the full path to each method.
  4. Add one or two dependencies to the class.
  5. Add a mapping function to get all of the blog articles via REST.
  1. Start the application and use Postman to access localhost:8080/api/blogposts. We are doing this to see the list of blog articles returned as a JSON list:

Outcome

The list of current blog articles will be available as a REST resource to query with Postman.


Go to https://bit.ly/2xezb1A to access the code for the BlogPostController.java file. Go to https://bit.ly/2NFjris to access the code for the BlogPostRestController.java file.
Go to https://bit.ly/2QrByao to access the activity/ folder. To refer to the detailed steps, go to the Solutions section at the end of this book on page 260.