Sitemap

Creating Your REST Controller; Using Postman to Test Requests

Donovan
4 min readFeb 12, 2022

part two to this.

Now that there is a RestController with the ability to map to functions, time to create the rest of the CRUD capabilities in the controller and test the resources using Postman.

Start by adding the GetMapping annotation to the Read method set the value endpoint using ‘{id}’. This lets the application know this variable will be provided each request. Define the method with a return type of Athlete, passing the id as a parameter. Use the appropriate service method to findAthlete from the repository, return new ResponseEntity with athlete and HttpStatus.

Spring provides a few more annotations necessary for providing metadata about the application structure; ‘@PathVariable and ‘@RequestBody’. PathVariable is used to read variables mapped to the url, or uri. RequestBody is used to map the body to the http request.

The Create method in Crud is used with POST requests that post, or save, objects to our database, mapping them to a table.The body of the request should contain the object that you want to save to the database; passed using the RequestBody annotation. Use the appropriate service method to addAthlete. Return new ResponseEntity, passing the new instance and HttpStatus.Created.

The Update method in Crud is sent with a PUT request. For simplicity, we will update the entire object with a certain id unique to our entity class. The PUT method is not posting a new request but updating one at a unique identity. Use the service method to updateAthlete in the repository. Return new ResponseEntity with updated Athlete and HttpStatus.OK.

The Delete method is sent with a DELETE request. Use the service method to delete an Athlete by id. Return a new ResponseEntity with the appropriate message.

Testing with Postman

This requires a connection to the database that is being used for the application. Start the mysql server, configure database settings in application.properties file, connect to the database using the database explorer, making sure the ping is successful and MySQL is connected. No errors should be returned running the main class of the application. For more on connecting the database, here.

Open the browser and navigate to Postman.com. Select Workspaces from the dropdown menu and create a new workspace. This is a space to organize applications and requests, through collections.

Create a collection, a group of requests. Name it appropriately. After collection is made, use the 3 dot menu icon to, ‘Add request’.

The GET method created in the controller will get tested in this request. Since the table is empty we should receive an empty list back from the database. Title the request “GET all athletes” or something similar. The default method of requests is GET. Enter the request url that set in the controller, including the base url for the class and the value set to the GetMapping method. Click save, then Send. The response body should send back an empty list.

To send a Post request send an object in the request body to the url mapped to the post mapping value. Add a new request to the collection, select POST as the method. Go to the Body -> select ‘raw’ -> JSON. Pass the object in JSON format. After sending the successful response is the athlete object.

To test the update method send a PUT request. Add a new request to the collection; sending a put request with the object you wish to update and the id of the entity instance to update.

The delete method is sent with the DELETE request and an id in the uri path. Add new request, select DELETE, send, and receive a successful response.

After sending a successful post request the request to get all Athletes should return the new instance. If the connection to the database is functioning and the post request was successful, the instance would have persisted in the database. Then test to update, read, and delete that instance.

This demo showed how to finish basic set up of the RestController and test those methods using Postman. This is the final step in building an API. This is a template that can be built upon to add specialized service functions, validations, authorizations, custom exception handling, and more. The following posts will be about various topics building on this template.

--

--

Donovan
Donovan

Written by Donovan

Learning. Putting up shots in React and Rails and anything else I get my hands on. Sights set on becoming an even more gainfully employed developer.

No responses yet