Build a REST API with Spring Boot — Part 3: Implementing Our Controller
In the previous tutorial, we talked about the importance of controllers in our REST API. We also created the model that we will be working with. We are now ready to start implementing our controller to perform CRUD Operations.
Implementing Our Controller
In your src/main/java directory, create a new Java Class named “BookController”.
- The
@RestController
annotation allows us to avoid annotating each of the methods of the controller with@ResponseBody
. It also indicates that the data returned by each method will be written straight into the response body. - Since we are implementing CRUD operations, we will be using the following HTTP methods : GET (To retrieve one or all books), POST ( To create a new book instance), PUT (To update an existing book), DELETE (To delete a book).
- We are using the annotations :
@GetMapping
,@PostMapping
,@PutMapping
and@DeleteMapping
, corresponding to each of the preceding methods. - A path is passed to each of the annotations. Hence, the endpoints of our API are :
- GET : /books
- GET : /books/{id}
- POST : /books
- PUT : /books/{id}
- DELETE : /books/{id}
- The different methods such as :
findAll()
,save()
,findById()
are already implemented within the Spring Data JPA Framework. We just need to call them to perform the required operations. - The
BookNotFoundException
is an exception used to indicate when a book is looked up but not found
In your src/man/java directory, create a new Java Class named “BookNotFoundException”.
That’s it! We are done writing code for our Controller and hence we just finished developing our REST API!
Go to your Main Class and start the application !
Testing The REST API
In order to test the functionalities of our REST API, I recommend you installing Postman. It is a great tool for sending different HTTP Requests.
Once you have installed Postman, open it.
Creating New Books
In order to perform this task, we need to send a POST request to the URL : /books
. The body of the request will contain the information of the new book in JSON format. For instance :
{“id”: 1,“title”: “Foundation”,“author”: “Isaac Asimov”,“genre”: “Science Fiction”}
In Postman, select the POST method and enter the following URL: http://localhost:8080/books
After that select the “Body” Tab, select “raw” format and instead of “Text” choose “JSON”. Press the “Send” Button. You will receive a response in JSON format. It contains the information that you just sent. That just means that the new book has been successfully inserted into the database.
List All Of The Books
In order to perform this task, we need to send a GET request to the URL : /books.
Select GET Method in Postman and press the “Send” button.
To give you a better understanding of this method, I have added more books to database.
You will receive the following JSON response :
You can see that the books previously created have been inserted into the database.
Get One Single Book
In order to retrieve information regarding one single book, we have to send a GET request to /books/{id}
where “id” is the unique identifier of the book.
For instance, let’s retrieve the information concerning the book N° 2 .
Send a GET Request to the following URL : http://localhost:8080/books/2
Press the “Send” Button.
You will receive the following JSON Response :
Update A Book
In order to perform this task, we need to send a PUT request to /books/{id}
where “id” is the unique identifier of the book. For instance, let’s update the information of the book N° 3. I want to replace it with the following book :
Select PUT in Postman and enter the following URL : http://localhost:8080/books/3
In the body of the request, enter the information of the updated book in JSON format. After that, press the “Send” button.
You will get a JSON Response that contains the updated book in JSON format. It means that the book has been successfully updated.
Delete A Book
In order to perform this task, we need to send a DELETE request to /books/{id}
where “id” is the unique identifier of the book. For instance let’s delete the book N° 1.
Select DELETE in Postman and enter the following URL : http://localhost:8080/books/1
Press the “Send” button.
The book has been removed now. In order to verify that. We need to list all the books.
GET : http://localhost:8080/books/
Final Thought
I hope you got a good idea about Spring Boot and REST APIs. Thank you for following this tutorial.