others-Postman and SpringBoot RESTful api test example
1. The purpose of this post
This post would demo how to use postman to test springboot restful api. I would demo how to use postman to test the restful api(http get/post) step by step.
If you want to use command line test tool(like ab) to test the apis, just refer to this article:
Dependencies:
- SpringBoot 1.x or 2.x
- PostMan 7.3.6
- Jdk 1.8+
2. The api
For example , we developed the following restful api by using springboot:
The domain object:
The service object:
The DAO object:
And the application.properties file content:
And the test data in database:
Now we would have a restful url to test: http://localhost:8080/studentsApi/queryByPage
3. How to use postman to test the restful api(http get)–step by step
You can install postman from this site
Then ,open it and create a basic request like this:
3.1 Open postman and click the ‘create’ button
- At first, click the ‘new’ button
- Then, click the ‘Get Request’, because we have created an api with GetMapping
3.2 Fill in the form to create the Get Request
Input the request name and save it into a collection , if no collection exists, just create one.
Now we would get an empty postman request:
3.3 Send a simple restful get request via postman to springboot service
Now we input the url to test the api:
The url is http://localhost:8080/studentsApi/queryByPage?page=2
Click ‘send’ button, then we would get to the breakpoint:
As the arrow shows, there are three variables in the http request: size,page and sort
Run across the breakpoint and we would get this result in postman:
4. How to use postman to test the restful api(http post json)–step by step
Ok, test springboot http get api is easy with postman, but how to test the http-post-api with postman?
4.1 Add http post api to codes
- (1) Add this code to controller:
It contains two features:
- it has a new endpoint named ‘/newStudent’ which accepts an json object and call the service to save it
-
it has a new endpoint named ‘/student/{id}’ which accepts an id and return the specified student object as json
- (2) Add this code to service
It just call the Spring data JPA to save and query the Student object.
4.2 Test the post api with postman
-
(1) Create a new http request in postman Make sure to choose the POST http method.
-
(2) Input the post url and headers
SpringBoot restful post api needs the following http headers to be present:
- Content-Type
- application/json
- Accept
- application/json
It means the server needs the client to transfer object as json.
- (3) Input the http post body Instead of using the http url to pass request body, http post protocol use http body to transfer the request body.
If you don’t want to manualy compose the json string, just nagivate to http://localhost:8080/studentsApi/student/1,you would get a json template of the Student class.
- (4) Click send You would get this reponse:
You can see that the new student has been saved to the database and it has an id of 12.
5. Conclusion
As you can see ,using postman to test the springboot restful api is very easy, you can get more help on this topic by visiting this site.