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…

--

--

Donovan

Learning. Puttin up shots in Angular and Java and Python and anything else I get my hands on. Sights set on becoming a gainfully employed developer.