The first example I am going to explain is about HTTP GET request, the second example will be about HTTP POST request, the third example about HTTP PUT request, and the fourth example is for HTTP DELETE request. In all of these examples, I am going to use JSON Representation.

This is a Spring Boot main class. A Spring Boot REST application loads through this class. We can also see that this class is created with the annotation @SpringBootApplication . As per the Spring documentation, the annotation @SpringBootApplication is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan, and these annotations are frequently used together. Most of the time, in Spring Boot development, the main class is always annotated with all three of these important annotations.

So we are going to modify the @SpringBootApplication (given below in the Java class) with a component path. Without that, the application cannot find out the controller classes. We will learn more about controller classes in a few minutes.

8. Now we will introduce two controllers, one to serve the GET request and the second one to serve the POST request. With the GET request, we will retrieve all Student Registration information, and with the POST request, we will add student information into our application. In spring’s approach to build a RESTful web services, HTTP requests are handled by a controller. Controller classes/components are easily identified by the @RestController annotation, and the below StudentRetrieveController will handle GET requests for /student/allstudent by returning a list of Student class objects in JSON format.

Now just follow step 4 and step 5 and create the package com.bhaiti.kela.controllers and add the class StudentRetrieveController to it and import the class Student and modify the class like below:-

Till now, we are done with everything for a GET and POST RESTful call. Let’s test this application first. After the test, we will learn about PUT and DELETE calls as well. First, we need to compile the application.

Compilation: To compile this project with Maven, we will add the below information into the POM file: Double click and open the POM file from project explorer window and add below information under “<dependencies>” section.

9. Now open file application.properties under C:\Projects\spring-boot-rest-2\src\main\resources and add the below lines in it:

server.port=8083

spring.profiles.active=@spring.profiles.active@

10. Now open the command prompt window and go to the project home directory in the command prompt. In my case, it looks like this:

cd C:\Projects\spring-boot-rest-2

mvnw clean package

If everything goes fine, you can see the below result:

Now run the server:

java -jar target\spring-boot-rest-2-0.0.1-SNAPSHOT.jar

11. Once the server starts successfully, we will test get GET request first. Open your SOAPUI tool. In SOAPUI tool from file menu select New REST Project and put the below URL in the address bar and press OK. (File=>New REST Project)

Now you can see the above information. The reason is our student list is empty at the moment, and to store student information, we need to insert some values in it. For that, we will use the POST service that we have already implemented.

13. Follow step 10 and insert the below URL in the address box and click OK.

http://localhost:8083/register/student

14. Now you have to select POST this time from Method combo box for a post request. Once you select POST Media Type, sub pane will emerge from where you have to select media type to application/json like below and put the below json body in it and click the green arrow button, which will add the information into our application.

15. Now go to the GET request project (step 12) and click the green arrow button. You can see one record like below:

16. Now go back to POST request test (step 14) and add at least three records and call the GET request (step 11) and see:

Until now, we have learned how to implement GET and POST services. Now we will learn PUT and DELETE requests.

17. Now introduce the controller classes to handle PUT and DELETE requests. Follow Step 6 above and add the below two controller classes in it.

NB: In reality, you don’t need four controller classes to handle four different REST service calls. One controller class will suffice, but for clarity, I have introduced four different controller classes in this article.

18. Now stop the server (by using Control-C), and compile the code, and run the server again.

19. First, insert three to four records into the system by using POST call and retrieve the records by using GET test mentioned in step 12.

20. Now we will modify a record here, for that create a new REST project in SOAPUI tool and use the below URL and this time select PUT from method combo box and modify the records as shown below:

http://localhost:8083/update/student

Once you click green arrow button this information will be updated in the system. Now see the result, just do the GET call and check.

21. Now finally we will do the DELETE service call test. Just create a REST project in SOAPUI and use below URL and select DELETE from method combo box and enter the record like below and click the green arrow button and to check the final outcome just call the GET service call.

http://localhost:8083/delete/student/12346 (the last numeric value is registrationNumber)

If you liked this article, please do not forget to click the like button and let me know your thoughts in the comments section. Thanks!